Python 练习册,每天一个小程序,原题来自Yixiaohan/show-me-the-code
我的代码仓库在Github
任一个英文的纯文本文件,统计其中的单词出现的个数。
该题目代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
\"\"\"
任一个英文的纯文本文件,统计其中的单词出现的个数。
\"\"\"
import re
def count_words(file_path):
with open(file_path) as file:
text = file.read()
words = re.findall(r\'[a-zA-Z]+\', text)
count = len(words)
return count
print(count_words(\'text.txt\'))