2017-10-11 45 views
1

CSV文件我有脚本读取CSV文件中的数据,并使用probablepeople库结果和probablepeople有方法即parse()我使用它像probablepeople.parse('some text'),这是产生这样的 输出例如probablepeople.parse()数据在python3

some text  | return by probablepeople.parse() 
Justin Nappier | [('Justin', 'GivenName'), ('Nappier', 'Surname')] 
Richard Urnberg | [('Richard', 'CorporationName'), ('Urnberg', 'CorporationName')] 

所以输出基本上这

[('text', 'columnname'), ('text', 'columnname'), ('text', 'columnname'), ('text', 'columnname')........] 

我想存储在CSV文件中的数据可以请你帮我这。

我的代码是这样的

# -*- coding: utf-8 -*- 

import probablepeople 
import csv 


try: 
    # Getting ASINs from the given csv and put into the start_urls 
    with open('full_name_submit.csv', 'rbU') as csv_file: 
     data = csv.reader(csv_file) 
     for url in data: 
      print(url[1]) 
      pr = probablepeople.parse(url[1]) 
      print(pr) 
      # some logic to put data to csv 

except Exception as e: 
    print(e) 
+0

使用probablepeople安装使用图书馆'PIP安装probablepeople' –

+1

你使用csv.reader了。为什么不同样使用[csv.writer(https://docs.python.org/2/library/csv.html#writer-objects) – ShreyasG

+0

你能张贴的CSV所需的输出文件?您是否有意将'tuple'的'list'转换为CSV,其中'tuple'的第二个位置决定了CSV列,并且它的第一个值是?如果是这样,在你的例子中'公司名称'是重复的,所以你会松散的信息。 – Adirio

回答

0

我没有在CSV东西太多经验,但使用join()方法将其转换为字符串可能有帮助。这样的事情可能吗?

''.join(str(x) for x in pr) 
+0

这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/18586018) – thewaywewere

0

','.join((x[0] for x in pr))

或任何你的分隔符。