0

データセットをインポートして、テキスト分析を出力しようとしています。ただし、データの最後の列を出力することしかできません。すべてのコード行を取得するには、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)
4

2 に答える 2

0

output=zip(row)削除して置き換えることをお勧めしますwriter.write(row)

取り外してループの上writer.writerows(output)に置きます。writer = csv.writer(open('csvoutput.csv', 'wb'))

于 2013-10-23T17:56:52.837 に答える
0

あなたの問題はこの行にあります:

output=zip(row)

なぜあなたがやっているのかわからないが、繰り返しごとzipに上書きoutputしていることは知っている。

ループの前に csv ライターを作成することをお勧めします。次に、ループの最後のステートメントとして、次のようにします。

writer.writerow(row)
于 2013-10-23T17:54:56.250 に答える