Python 練習(xí)冊柳刮,每天一個小程序,原題來自Yixiaohan/show-me-the-code
我的代碼倉庫在Github
目標(biāo)
任一個英文的純文本文件罪塔,統(tǒng)計其中的單詞出現(xiàn)的個數(shù)掌敬。
解決方案
該題目代碼如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
任一個英文的純文本文件孙援,統(tǒng)計其中的單詞出現(xiàn)的個數(shù)乘碑。
"""
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'))