【发布时间】:2016-03-23 20:22:13
【问题描述】:
如果 col1 等于之前行中的相同值,我正在尝试连接 col3,然后将输出写入新文件。我有一个如下所示的 CSV 文件:
col1,col2,col3
a,12,"hello "
a,13,"good day"
a,14,"nice weather"
b,1,"cat"
b,2,"dog and cat"
c,2,"animals are cute"
我想要的输出:
col1,col3
a,"hello good day nice weather"
b,"cat dog and cat"
c,"animals are cute"
这是我尝试过的:
import csv
with open('myfile.csv', 'rb') as inputfile, open('outputfile.csv','wb') as outputfile:
reader=csv.reader(inputfile)
writer=csv.writer(outputfile)
next(reader)
for row in reader:
while row[0]==row[0]:
concat_text=" ".join(row[2])
print concat_text
writer.writerow((row[0],concat_text))
它运行但我没有输出。帮助表示赞赏。
【问题讨论】:
-
while row[0]==row[0]: ...永远不会前进,这是一个无限循环。