本帖最后由 MyModHeaven 于 2022-8-20 15:24 编辑
报什么错?
另外,这样好看
[Python] 纯文本查看 复制代码 def getText("红楼梦.txt"):
f = open("红楼梦.txt","r",encoding="utf-8")
text = f.read()
f.close()
return text
import jieba
def wordFreq(红楼梦.txt,text,200):
words = jieba.lcut(text.strip())
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = true)
f = open(filepath[:-4]+'_词频.txt',"w")
for i in range(topn):
word, count = items[i]
f.writelines("{}\t{}\n".format(word,count))
f.ciose() |