问题描述: 任一个英文的纯文本文件,统计其中的单词出现的个数。
需求分析:
- 数据通过文件读取
- 读取文件中的单词
- 统计每个单词的个数
代码编写:
'''
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}')