# 第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。 import re re_word=re.compile(r'[a-zA-Z]+') def count_words(object): count = 0 for line in object: nums=re_word.findall(line) count+=len(nums) return count if __name__=="__main__": filename="count_words.txt" with open(filename) as outfile: print(count_words(outfile)) 问题:1.如果存在字符为ex2,该语句会识别ex,但实际中我需要放弃识别这个字符串,我应该怎么办呢?(加入程序做预处理,然后再进行?)
每天一python 题 0004
最新推荐文章于 2021-05-09 21:54:02 发布