import re
with open("text.txt") as f:
#读取文件中的字符串
txt = f.read()
#去除字符串中的标点、数字等
txt = re.sub('[,\.()":;!@#$%^&*\d]|\'s|\'', '', txt)
#替换换行符,大小写转换,拆分成单词列表
word_list = txt.replace('\n',' ').replace(' ',' ').lower().split(' ')
dic = {}
for word in word_list:
#统计字典中的词频
if word in dic.keys():
dic[word] += 1
else:
continue
#按照单词出现次数排序
word_count_dict = sorted(word_count_dict.items(), key=lambda x:x[1], reverse=True)
#输出到文件
with open("word_count.txt", 'w')as f1:
for i in word_count_dict:
f1.write("%s\t%s\n" %(i[0],str(i[1])))