项目构成:
实现代码:
#encode:UTF-8
import re
from tabulate import tabulate#调用tabulate模块打印表格
def get_word(txt):
vlist=re.split('[!,.\'":;\s\n]\s*',txt)
vdic=dict()
for vword in vlist:
if vword in vdic:
vdic[vword] += 1
else:
vdic[vword] = 1
#排序
vdic_sort=sorted(vdic.items() , key= lambda itme:itme[1] , reverse=True)
#返回值是元组列表
return vdic_sort
if __name__=="__main__":
#在这更换要统计的文本
with open('text.txt','r') as f:
txt=f.read()
word_sort = get_word(txt)
print("列出文本中的英文单词:\n")
headers=["WORD","TIMES"]
date=word_sort
table = tabulate(date, headers, tablefmt="pretty")
print(table)
项目效果展示: