源代码是这样的:
import jieba
txt = open("threekingdoms.txt", "r").read().decode('UTF-8')
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
运行出现错误:Traceback (most recent call last):
File "D:\编程\python\代码\实例10中文词频统计1.py", line 3, in <module>
txt = open("threekingdoms.txt", "r").read().decode('UTF-8')
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb1 in position 5: illegal multibyte sequence
要读取的是中文文本,不知道为什么会出错,求大佬解获!谢谢!