3、文件的综合运用
从给定的英文文件中读取所有内容按行存储到一个列表里面,统计文件中英文单词°are’出现的次数,要求上述功能用一个西数实现,西数名cuntistr_fileName.word),参数 str_fileName表示文件路径,word 表示要查询的单词,函数返回值为要查询的单词的出现个数。
import string
def cunti(str_fileName,word):
cnt=0
with open(str_fileName, 'r') as f:
result_dict = {}
strip = string.whitespace + string.punctuation
for line in f.readlines():
listWords = line.lower().split() # 不区分大小写的情况
for eachLetter in listWords:
if eachLetter==word:
cnt+=1
return cnt
print(cunti('1.txt',"are"))