我是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