一、题目要求
二、代码实现
python
f = open("D:\\workspace\\word.txt" , "r", encoding= "UTF-8")
# 方案一
# content = f.read()
# count = content.count("itheima")
# print(f"itmeiha在文件中出现了:{count}次")
# 方案二
num = 0
for line in f:
line = line.strip()
# 去除开头结尾的空格及换行符 (输出结果每行最后都加上了" \n ")
words = line.split(" ")
for word in words:
if word == "itheima":
num += 1
print(num)
f.close()