300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python 利用jieba读取txt文本进行分词后存入新txt

python 利用jieba读取txt文本进行分词后存入新txt

时间:2023-06-10 23:54:14

相关推荐

python 利用jieba读取txt文本进行分词后存入新txt

python 利用jieba读取txt文本进行分词后存入新txt

import jiebatxt = open("news.txt",encoding='UTF-8').read()words = jieba.lcut(txt)result = open('results.txt','w',encoding='UTF-8')result.write(' '.join(words))counts = {}for word in words:if len(word) == 1:continueelse:counts[word] = counts.get(word,0) + 1items = list(counts.items())items.sort(key=lambda x:x[1], reverse=True)for i in range(30):word, count = items[i]print (u"{0:<10}{1:>5}".format(word, count))result.write((str(items[i])))result.close()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。