博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python统计数据的频率
阅读量:6942 次
发布时间:2019-06-27

本文共 1002 字,大约阅读时间需要 3 分钟。

hot3.png

# -*- coding: UTF-8 -*- #!/usr/bin/env pythonfrom collections import Counterimport collections import jieba.analyseimport jiebaimport timeimport reimport sys#去除停用词#stopwords = {}.fromkeys(['的', '包括', '等', '是'])stopwords = {}.fromkeys([ line.strip() for line in open("stopwords.txt") ])#读取文件路径bill_path = r'article_nohtml.txt'#写入文件路径bill_result_path = r'result.txt'#读取文件with open(bill_path,'r') as fr:	all_the_text = fr.read()#处理特殊字符all_the_text = re.sub("\"|,|\.", "", all_the_text)#分词data = jieba.cut(all_the_text)#计算频率data = dict(Counter(data))#以词频排序def sort_by_count(d):      #字典排序      d = collections.OrderedDict(sorted(d.items(), key = lambda t: -t[1]))      return ddata = sort_by_count(data)  #将结果集写入文件with open(bill_result_path,'w') as fw:    for k,v in data.items():	k = k.encode('utf-8')	#处理停用词	if k not in stopwords:	#写入结果		#fw.write(str(k)+':'+str(v)+'\n')		#fw.write("%s,%d\n" % (k,v)) 		fw.write(str(k)+':%d'%v + '\n')#关闭流fw.close()

运行结果图

155930_Eelr_2400848.png

转载于:https://my.oschina.net/Tsher2015/blog/997849

你可能感兴趣的文章
Code Signal_练习题_depositProfit
查看>>
Oracle数据库—— 存储过程与函数的创建
查看>>
由于行255而未能重新格式化文档。已还原为原始格式。
查看>>
MVC的view页面内嵌C#语法发现路径被转码的解决方法
查看>>
PMBOK项目管理PMI主义\IPMA概述
查看>>
vscode中使用node服务调试,会在promise的reject出现断点报错
查看>>
SilverLight之向后台请求数据-WebClient
查看>>
2008server安装Intel I217V网卡驱动
查看>>
安卓应用获取权限判断
查看>>
hdu 1695(欧拉函数 容斥定理)
查看>>
CentOS 6.7安装MySQL
查看>>
Docker容器互联
查看>>
Linux基础知识--文件操作
查看>>
java+jsp+mysql网页制作总结(3)
查看>>
DAY6-小变化(java提示框)-2018-1-16
查看>>
mysql在表的某一位置增加一列、删除一列、修改列名
查看>>
iOS 网络与多线程--3.异步Get方式的网络请求(非阻塞)
查看>>
Excel相同内容如何设置相同的背景色
查看>>
LCA问题
查看>>
计算机基础知识
查看>>