在python for csv中添加新行以打印列输出
我对python相当陌生,正在尝试确定实现这一点的最佳方法。我有以下代码。在python for csv中添加新行以打印列输出,python,csv,Python,Csv,我对python相当陌生,正在尝试确定实现这一点的最佳方法。我有以下代码。 import csv from collections import defaultdict columns = defaultdict(list) with open('test.csv') as f: reader = csv.DictReader(f) for row in reader: for (k,v) in row.items(): columns[k].appen
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
print(columns['Created'])
print(columns['Person'])
print(columns['Comment'])
Created,UnwantedData,Person,Bogus,Comment
4-15-17,test,Chad,w,There is an update
4-16-17,for,Marlon,x,There is no update
4-17-17,this ,Kevin,y,I have reviewed the files
4-18-17,issue,Dan,z,I hate this place.
['4-15-17', '4-16-17', '4-17-17', '4-18-17']
['Chad', 'Marlon', 'Kevin', 'Dan']]
['There is an update ', 'There is no update', 'I have reviewed the files ', 'I hate this place. ']
4-15-17
Chad
There is an update
4-16-17
Marlon
There is no update
4-17-17
Kevin
I have reviewed the files
4-18-17
Dan
I hate this place.
我正在对下面的CSV文件输出运行它。
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
print(columns['Created'])
print(columns['Person'])
print(columns['Comment'])
Created,UnwantedData,Person,Bogus,Comment
4-15-17,test,Chad,w,There is an update
4-16-17,for,Marlon,x,There is no update
4-17-17,this ,Kevin,y,I have reviewed the files
4-18-17,issue,Dan,z,I hate this place.
['4-15-17', '4-16-17', '4-17-17', '4-18-17']
['Chad', 'Marlon', 'Kevin', 'Dan']]
['There is an update ', 'There is no update', 'I have reviewed the files ', 'I hate this place. ']
4-15-17
Chad
There is an update
4-16-17
Marlon
There is no update
4-17-17
Kevin
I have reviewed the files
4-18-17
Dan
I hate this place.
我只想打印创建的、人物和评论部分,但我得到的是下面的内容。
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
print(columns['Created'])
print(columns['Person'])
print(columns['Comment'])
Created,UnwantedData,Person,Bogus,Comment
4-15-17,test,Chad,w,There is an update
4-16-17,for,Marlon,x,There is no update
4-17-17,this ,Kevin,y,I have reviewed the files
4-18-17,issue,Dan,z,I hate this place.
['4-15-17', '4-16-17', '4-17-17', '4-18-17']
['Chad', 'Marlon', 'Kevin', 'Dan']]
['There is an update ', 'There is no update', 'I have reviewed the files ', 'I hate this place. ']
4-15-17
Chad
There is an update
4-16-17
Marlon
There is no update
4-17-17
Kevin
I have reviewed the files
4-18-17
Dan
I hate this place.
我试图通过在每次打印输出之间添加新行,使输出看起来像这样。
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
print(columns['Created'])
print(columns['Person'])
print(columns['Comment'])
Created,UnwantedData,Person,Bogus,Comment
4-15-17,test,Chad,w,There is an update
4-16-17,for,Marlon,x,There is no update
4-17-17,this ,Kevin,y,I have reviewed the files
4-18-17,issue,Dan,z,I hate this place.
['4-15-17', '4-16-17', '4-17-17', '4-18-17']
['Chad', 'Marlon', 'Kevin', 'Dan']]
['There is an update ', 'There is no update', 'I have reviewed the files ', 'I hate this place. ']
4-15-17
Chad
There is an update
4-16-17
Marlon
There is no update
4-17-17
Kevin
I have reviewed the files
4-18-17
Dan
I hate this place.
当您打印
列['Created']
时,您将打印创建列中存储的所有内容。相反,您只需要遍历列中的每个项
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k, v) in row.items():
columns[k].append(v)
for i in xrange(len(columns['Created'])):
print(columns['Created'][i])
print(columns['Person'][i])
print(columns['Comment'][i] + "\n")
“\n”将导致在注释部分后打印新行。我是否可以建议一种替代方法,您只需创建一个带有
dict
的新条目列表,只需将其筛选为只包含所需的键
import csv
wanted = ('Created', 'Person', 'Comment')
with open('test.csv') as f:
rdr = csv.DictReader(f)
result = []
for row in rdr:
result.append({k: v for k, v in row.items() if k in wanted})
for r in result:
print(r['Created'])
print(r['Person'])
print(r['Comment'])
print('')
如果您希望使用现有代码,只需使用zip
重新加入每个列表中的项目即可
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('test.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for k, v in row.items():
columns[k].append(v)
params = [columns['Created'], columns['Person'], columns['Comment']]
for cre, pers, cmt in zip(*params):
print(cre)
print(pers)
print(cmt)
print('')
可以在一次迭代中获得结果。这样可以节省时间
import csv
from collections import defaultdict
with open('test.csv') as f:
reader = csv.DictReader(f)
for row_dict in reader:
print "%s\n%s\n\n" % (row_dict['Created'], row_dict["Person"], row_dict['Comment'])