import jieba txt=open('聊斋志异.txt',encoding='gb18030',errors = 'ignore') words=jieba.lcut(txt.read()) counts={} for word in words: if len (word)==1: continue else: counts[word]=counts.get(word,0)+1 item=list(counts.items()) item.sort(key=lambda x:x[1],reverse=True) for i in range(20): print("{0:<10}{1:>5}".format(item[i][0],item[i][1]))