python之西游记字数统计
2017-01-09 20:38
381 查看
# -*- coding: utf-8 -*- import sys,string,json reload(sys) sys.setdefaultencoding('utf8') fr=open('xyj.txt','r') characters=[] stat={} for line in fr: line=line.strip() if len(line)==0: continue #print type(line) line=unicode(line) #print type(line) for x in xrange(0,len(line)): if line[x] in [' ', 4000 '\t','\n',',','.','。','!',':','“','”','?']: continue if not line[x] in characters: characters.append(line[x]) if not stat.has_key(line[x]): stat[line[x]]=0 stat[line[x]]+=1 fw=open('result.json','w') fw.write(json.dumps(stat)) fw.close() stat=sorted(stat.iteritems(),key=lambda d:d[1],reverse=True ) print type(characters[0]) for x in xrange(0,20): print characters[x] print '********************************************' print type(stat[0][0]) for x in xrange(0,20): print stat[x][0],stat[x][1] fw=open('result.csv','w') for item in stat: fw.write(item[0]+':'+str(item[1])+'\n') fw.close() fr.close()
输出:
<type 'unicode'> 吴 承 恩 著 第 一 回 灵 根 育 孕 源 流 出 心 性 修 持 大 ******************************************** <type 'unicode'> 道 10023 不 7984 了 7144 一 7079 那 6934 我 6575 是 5907 行 5474 来 5431 他 5297 个 5206 你 5086 的 4971 者 4887 有 3909 大 3603 得 3514 这 3481 去 3377 上 3260 [Finished in 19.7s]
同时输出json和csv
相关文章推荐
- [置顶] 【python 文本统计】西游记用字统计
- Python文本统计功能之西游记用字统计操作示例
- C++ Boost 之Python(生成一个扩展模块)
- Python指南--数据结构
- Python指南--数据结构
- 搭建Python的集成开发环境WingIDE
- Python指南--深入流程控制
- Python指南--深入流程控制
- 用Python实现数据库编程
- Python指南--开胃菜
- Python指南--开胃菜
- Python指南--初步认识Python
- Python指南--前言
- Python指南--使用Python解释器
- Python指南--使用Python解释器
- python库lxml在linux和WIN系统下的安装
- C++ Boost 之Python(一个简单的例子)
- C++ Boost 之Python(继承)
- C++ Boost 之Python(大纲)