我是Python新手。我正在嘗試讀取CSV文件,並從文件中刪除停用詞後,我必須將其存儲到新的CSV文件中。我的代碼是刪除停用詞,但它將第一行復制到單行文件的每一行。 (例如,如果文件中有三行,則它將在第一行中將第一行復制三次)。如何逐行讀取CSV文件並將其每次存儲到新行中的新CSV文件?
正如我分析它,我認爲問題是在循環中,但我沒有得到它。我的代碼附在下面。
代碼:
import nltk
import csv
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
def stop_Words(fileName,fileName_out):
file_out=open(fileName_out,'w')
with open(fileName,'r') as myfile:
line=myfile.readline()
stop_words=set(stopwords.words("english"))
words=word_tokenize(line)
filtered_sentence=[" "]
for w in myfile:
for n in words:
if n not in stop_words:
filtered_sentence.append(' '+n)
file_out.writelines(filtered_sentence)
print "All Done SW"
stop_Words("A_Nehra_updated.csv","A_Nehra_final.csv")
print "all done :)"
這不是很清楚,你應該表現出輸入,電流輸出和預期輸出的一個例子。 – polku