-- coding=utf-8 --
file=open(‘test.txt’,encoding=’utf-8’)
print(file)
content=file.read()
print(content)
file.close()
import string
from matplotlib import pyplot as plt
def show_words(num):
with open('test.txt',encoding='UTF-8') as f:
content = f.read()
# 使用replace方法替换标点符号
word_list=content.split()
words=[]
#遍历列表
for word in word_list:
word=word.strip(string.punctuation+string.whitespace)
word=word.lower()
words.append(word)
# print(words)
hist={}
for word in words:
if word not in hist:
hist[word]=1
else:
hist[word]=hist[word]+1
# print(hist)
hist_list=[]
#字典:items() 分别获取到键值对的 键 与 值
for key,value in hist.items():
print('键:%s 值:%s'%(key,value))
hist_list.append((value,key))
hist_list.sort(reverse=True)
# print(hist_list)
for ys in hist_list[:num]:
plt.bar(ys[-1],ys[0])
plt.legend #代表数据传输完毕
plt.title('word_anls')
plt.xlabel('words')
plt.ylabel('times')
plt.show() #设置图表显示
if name == ‘main‘:
nums = int(input(‘请输入你想得到单词的个数:’))
show_words(nums)
#排序,根据每个单词出现的次数排序
#[] 方法 sort
# 格式化为[(18:the),(9:a)]