def get_text(file_name): with open(file_name,'r') as fr: text = fr.read() text = text.lower()#注意会有大小写之分,这里不区分大小写 delete_ch = ['\n',':','!','?',',','.']#要删除的标点 for ch in delete_ch: text = text.replace(ch,' ') return text file_name = 'test.txt'#要处理的文件 text = get_text(file_name) text = text.split()#注意!不能有split(' ') count_dict = {} #统计每个单词的个数 for i in text: count_dict[i] = count_dict.get(i,0) + 1 #转化成列表的形式 count_dict = list(count_dict.items()) count_dict.sort(key = lambda x:x[1], reverse = True) for i in range(10): word,count = count_dict[i] print(word,count)