#!/usr/bin/env python
# coding:UTF-8
"""
@version: python3.x
@author:曹新健
@contact: 617349013@qq.com
@software: PyCharm
@file: 统计文件中所有英文单词的数量.py
@time: 2019/1/1 20:12
"""
import re
words = {}
r = re.compile(r"[,!\*\.]")
with open("python_this.txt","r") as f:
for line in f:
for word in r.sub("",line.strip()).split(" "):
if word in words:
words[word] += 1
words.setdefault(word,1)
print(words)
python_this.txt:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never