2 回答
TA贡献1880条经验 获得超4个赞
这最终奏效了,感谢Rakesh
import re
from collections import Counter
def word_freq(file_tokens):
global count
for word in file_tokens:
count = Counter(file_tokens)
return count
f = open("german/test/polarity/negative/neg_word_list.txt")
clean = re.sub(r'[0-9]', '', f.read())
file_tokens = clean.split()
print(word_freq(file_tokens))
print("done")
f.close()
TA贡献1801条经验 获得超8个赞
进一步阅读我注意到你没有“阅读”文件,你只是打开了它。
如果您只打印打开文件:
f = open("german/test/polarity/negative/neg_word_list.txt")
print(f)
你会注意到它会告诉你对象是什么,“io.TextIOWrapper”。所以你需要阅读它:
f_path = open("german/test/polarity/negative/neg_word_list.txt")
f = f_path.read()
f_path.close() # don't forget to do this to clear stuff
print(f)
# >>> what's really inside the file
或者没有“close()”的另一种方法:
# adjust your encoding
with open("german/test/polarity/negative/neg_word_list.txt", encoding="utf-8") as r:
f = r.read()
这样做可能不会在列表中,而是在纯文本文件中,因此您可以迭代每一行:
list_of_lines = []
# adjust your encoding
with open("german/test/polarity/negative/neg_word_list.txt", encoding="utf-8") as r:
# read each line and append to list
for line in r:
list_of_lines.append(line)
添加回答
举报