我正在导入一个数据集,并尝试输出一些文本分析。但是,我只能让它输出最后一列数据。我应该把csv.writer放在哪里,以便放入所有的代码行?
from __future__ import division
import csv
import re
from string import punctuation
faithwords = ['church', 'faith', 'faith']
with open('dataset.csv', 'rb') as csvfile:
data = csv.reader(csvfile, delimiter=",")
for row in data:
faithcounter = 0
row3 = row[3]
row3 = row3.lower().replace(' ', ' ')
row4 = row[4]
row4 = row4.lower().replace(' ', ' ')
for p in list(punctuation):
row3 = row3.replace(p, '')
row4 = row4.replace(p, '')
essay1= re.split(' ', row3)
essay2= re.split(' ', row4)
essay1len = len(essay1)
essay2len = len(essay2)
num_of_rows = len(row)
for word in essay1:
if word in faithwords:
faithcounter = faithcounter + 1
for word in essay2:
if word in faithwords:
faithcounter = faithcounter + 1
totallen = (essay2len + essay1len)
row.append(essay1len)
row.append(essay2len)
row.append(totallen)
row.append(faithcounter)
row.append(faithcounter / totallen)
output = zip(row)
writer = csv.writer(open('csvoutput.csv', 'wb'))
writer.writerows(output)发布于 2013-10-24 01:54:56
您的问题出在下面这一行:
output=zip(row)我不确定您为什么要使用zip,但我知道您在每次迭代中都会覆盖output。
我建议您在循环之前创建csv编写器。然后,作为循环中的最后一条语句,执行以下操作:
writer.writerow(row)发布于 2013-10-24 01:56:53
我建议删除output=zip(row),代之以writer.write(row)
删除writer.writerows(output)并将writer = csv.writer(open('csvoutput.csv', 'wb'))放在循环之上。
https://stackoverflow.com/questions/19548780
复制相似问题