# import jieba## # 读取文件# file1 = open("test.txt", "rb").read()# # 统计词数# words = jieba.lcut(file1)## # 定义一个空字典# counts = {}# for word in words:# if len(word) == 1:# continue# else:# counts[word] = counts.get(word, 0) + 1# items = list(counts.items())# # 排序# items.sort(key=lambda x: x[1], reverse=True)# resultFile = open("outPut.txt", "a+", encoding="utf-8")# for i in range(5):# word, count = items[i]# resultFile.write("{0:<10}{1:>5}\n".format(word, count))import jiebadef openFile(path):text = open(path, 'r', encoding='ANSI').read()return texttext = openFile('test.txt')words = jieba.lcut(text)lst = ['һ']dict = {}for word in words:if word.isalpha():if len(word) == 1:continueelse:dict[word] = dict.get(word, 0) + 1items = list(dict.items())items.sort(key=lambda x: x[1], reverse=True)str1 = ''for i in lst:for j in items:if j[0] == i:items.remove(j)for i in range(5):word, count = items[i]str1 += "{} : {}".format(word, count) + '\n'file = open("output.txt", 'w+', encoding='utf-8')file.writelines(str1)file.close
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。