我正在导入一个数据集并尝试输出一些文本分析。但是,我只能让它输出最后一列数据。我应该将 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)