我是python的新手并尝试在csv文件中执行停用词删除并将结果写入另一个csv。以下是我写的代码:
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import csv
import codecs
f = codecs.open("F:/mtech/Project/answer_sample.csv","r","utf-8")
answers = f.read()
f.close()
file_out = open('answer_swr','w')
unicode(answers)
#for x in answers:
# print("type of x is")
# print type(x)
#for y in stopwords.words('english'):
# print("type of y is")
for line in answers:
stop_words=set(stopwords.words("english"))
words=word_tokenize(line)
filtered_sentence=[""]
for n in words:
if n not in stop_words:
filtered_sentence.append(""+n)
file_out.writelines(filtered_sentence+["\n"])
print("Stop words removed..")
我收到一个错误:
file_out.writelines(filtered_sentence+["\n"])
TypeError: writelines() argument must be a sequence of strings