Python读书笔记-每日篇-20190301|单词统计

问题描述: 任一个英文的纯文本文件,统计其中的单词出现的个数。

需求分析:

  1. 数据通过文件读取
  2. 读取文件中的单词
  3. 统计每个单词的个数

代码编写:

'''
Created on 2019年3月1日

@author: Administrator
'''
import re
import collections

def word_list(filename):
    try:
        with open(filename,"r") as file:
            file_content = file.read()
    except Exception as e:
        print("FIle[%s] read Error!"%filename,e)
    else:
        pattern = re.compile(r'\W+')
        words = pattern.split(file_content)
        return words

def word_counter(filename):
    words = word_list(filename)
    word_collection = collections.Counter(words)
    return word_collection

if __name__ == "__main__":
    print(word_list("d:\\Magnets.txt"))
    word_colletion = word_counter("d:\\Magnets.txt")
    for word,word_count in word_colletion.most_common(18):
        print(f'{word:<20}:{word_count}')

猜你喜欢

转载自blog.csdn.net/lreis2010/article/details/88039088