import jieba
txt = open("D:\pythod\python weijian\聊斋", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 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:<5}{1:>5}".format(word, count))