""" 通过读取文件计算python出现的次数 """ # count = 1 with open('D:program\单词计数.txt', 'r', encoding="UTF-8") as f: # 方式1,读取全部内容 content = f.read() count = content.count("Python") print(count) # # 方法2 # count1 = 0 # for line in f: # line.strip()#去除开头结尾空格以及换行符 # words = line.split("") # for word in words: # if word == "Python": # count1+=1 # print(count1)