尝试获取代码以在Python中输出多个列

尝试获取代码以在Python中输出多个列,python,csv,writer,Python,Csv,Writer,我正在导入一个数据集并尝试输出一些文本分析。但是,我只能让它输出最后一列数据。我应该把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.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)

您的问题在于这一行:

output=zip(row)
我不知道你为什么要做
zip
,但我知道你每次迭代都会覆盖
输出

我建议您在循环之前创建csv编写器。然后,作为循环中的最后一条语句,执行以下操作:

writer.writerow(row)

我建议删除
output=zip(行)
,并用
writer.write(行)

删除
writer.writerows(输出)
并放置
writer=csv.writer(打开('csvoutput.csv','wb'))

在您的循环上方。

在循环结束时使用
zip(行)
您想实现什么?