我有2个文件:stopwords.txt
和a.txt
我想从文件stopwords.txt
的文件a.txt
中删除停用词,并用空格隔开。
我该怎么做?这是我尝试做的:
def remove_stopwords(review_words):
with open('stopwords.txt') as stopfile:
stopwords = stopfile.read()
list = stopwords.split()
print(list)
with open('a.txt') as workfile:
read_data = workfile.read()
data = read_data.split()
print(data)
for word1 in list:
for word2 in data:
if word1 == word2:
return data.remove(list)
print(remove_Stopwords)
预先感谢
答案 0 :(得分:0)
a.txt
:
good great bad
stopwords.txt
:
good bad
也许:
with open('a.txt','r') as f, open('stopwords.txt','r') as f2:
a=f.read().split();b=f2.read().split()
print(' '.join(i for i in a if i.lower() not in (x.lower() for x in b)))
答案 1 :(得分:0)
这里是一个例子:
k = []
z = []
with open('stopWords.txt', 'r') as f:
for word in f:
word = word.split('\n')
k.append(word[0])
with open('a.txt', 'r') as f_obj:
for u in f_obj:
u = u.split('\n')
z.append(u[0])
p = [t for t in z if t not in k]
print(p)
遍历停用词文件中的每个单词并将其附加到列表,然后遍历另一个文件中的每个单词。执行列表理解,并删除出现在停用词列表中的每个单词。