获取文本文件的内容 并统计英文小说内的单词个数以及用图表显示

-- 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)]

最终效果图最终效果图

猜你喜欢

转载自blog.csdn.net/qq_43074872/article/details/82178610