python用jieba库进行哈姆雷特词频统计

FileNotFOundError [ Errno 2] No such file or directory : hamlet . txt
r 。 read ()求解答

找不到文件,你现在应该使用的是相对路径的写法,保证txt文件和运行的脚本文件在同一目录下。文件名也用复制的方式,避免出错。
有帮助请采纳,还有不懂的可以继续追问~

刚写过一个类似的,应该就能用:

import jieba.posseg as psg
with open('d:\\novel.txt','r') as f:
    data=f.read()

seg = psg.cut(data)
words = [s.word for s in seg if s[1] and s[1]!='\n']

seg = psg.cut(data)
dic = {s.word:s.flag for s in seg if s[1] and s[1]!='\n'}
print('排序前的字典:\n',dic)

dic = {d:dic[d] for d in sorted(dic,key=lambda x:words.count(x))}
print('排序后的字典:\n',dic)