任一个英文的纯文本文件,统计其中的单词出现的个数123456789101112131415import stringf= open(r'C:\Users\12905\Desktop\1.txt', 'r+') #从文件中读取内容str1= f.read()str= str1.lower() #全部切换成小写list= str.split() #切片list1= []for i in range(0, len(list)): list[i]= list[i].strip(string.punctuation)for word in list: if word not in list1: list1.append(word)for word in list1: print(word, list.count(word))
运行结果如下图所示: