【发布时间】:2020-08-21 22:40:02
【问题描述】:
我正在使用 Python Counter 和 nltk。我正在尝试搜索外部文档上的单词,用stopwrods 删除一些单词,然后显示最常见的单词。但是出现了一个不知道怎么解决的错误:
AttributeError: 'Counter' object has no attribute 'write'
知道怎么继续吗?
from collections import Counter
import io
from nltk.corpus import stopwords
import nltk
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
#word_tokenize accepts a string as an input, not a file.
stop_words = set(stopwords.words('spanish'))
file1 = open("labs.txt")
line = file1.read()# Use this to read file content as a stream:
words = line.split()
for r in words:
if not r in stop_words:
with open("labs.txt") as input_file:
vonCount = Counter(word for line in input_file for word in line.split())
vonCount.write(" "+r)
print(vonCount.most_common(70))
【问题讨论】:
-
摆脱
vonCount.write(" "+r)这一行