python读取txt, jieba分词并统计词频后输出结果到Excel和txt文档

匿名 (未验证) 提交于 2019-12-02 22:51:30
1
2
3
4
5
open('1.txt''r') as fp:
=fp.readlines()
=[line.split()[1][9forinlines]
open('out.txt''w+') as out:
out.write('\n'.join(res))

#encoding=utf-8
import jieba
import jieba.posseg as pseg
import re
filename='result.txt'
fileneedCut='test.txt'
fn=open(fileneedCut,"r")
f=open(filename,"w+")
for line in fn.readlines():



f.close()

fn.close()


  1. import
  2. sys.setdefaultencoding('utf-8'
  3. import
  4. import
  5. import#写入Excel表的库
  6. if"__main__"
  7. 'ascii'
  8. "wordCount")#Excel单元格名字
  9. forin'1.txt'):#1.txt是需要分词统计的文档
  10. '\n\r').split('\t'#制表格切分
  11. 0#jieba分词
  12. forin
  13. "wordCount.txt",'w'#打开文件
  14. forin
  15. ifnotin#统计数量
  16. 1
  17. else
  18. 1
  19. True
  20. forin
  21. forin
  22. if
  23. +str(word_dict[key])+'\n'#写入txt文档
  24. 0
  25. forin
  26. 1
  27. 0
  28. 'wordCount.xls'

1.txt是你需要分词统计的文本内容,最后会生成wordCount.txt和wordCount.xls两个文件。下图是最后结果




易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!