Python 从CSV文件读取数据,执行停止字删除并写入另一个CSV文件

Python 从CSV文件读取数据,执行停止字删除并写入另一个CSV文件,python,Python,我是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

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

请不要张贴代码的图片,而是直接粘贴到问题中。这使得其他人更容易提供帮助。“停止单词删除”是什么意思?此外,你为什么不简单地检查你传递给函数
writelines
的输入,以及为什么输入不是字符串序列(根据你得到的错误,它应该是什么)?@PhillipD我已经发布了我的代码。顺便说一句,2,您希望通过
unicode(答案)
实现什么???这个表达式的结果立即被“破坏”。如果要将
答案
转换为unicode,则需要存储结果,即
答案=unicode(答案)
。请不要发布代码图像,而是直接将其粘贴到问题中。这使得其他人更容易提供帮助。“停止单词删除”是什么意思?此外,你为什么不简单地检查你传递给函数
writelines
的输入,以及为什么输入不是字符串序列(根据你得到的错误,它应该是什么)?@PhillipD我已经发布了我的代码。顺便说一句,2,您希望通过
unicode(答案)
实现什么???这个表达式的结果立即被“破坏”。如果要将
答案
转换为unicode,则需要存储结果,即
答案=unicode(答案)