Python读取txt文件,并统计英文单词出现次数
读取txt文件,一篇英文文章,统计该文章中某个单词的出现次数。
'''
读取txt文件,一篇英文文章,统计该文章中某个单词的出现次数。
'''
with open('article.txt', 'r', encoding='gbk') as file:
article = file.read()
word = input('请输入你想要查询的单词:')
print(f'{word}单词出现的次数为:', article.lower().count(word.lower()))
运行结果