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

2024-03-29 05:16:43 发布

您现在位置: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.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..")

我收到一个错误:

^{pr2}$

Tags: csvinfromimportfortypesentencefiltered