首页 > 其他 > 详细

对一个文本不同的字进行计数

时间:2018-05-28 11:09:09      阅读:157      评论:0      收藏:0      [点我收藏+]
import json
fr =open(‘xyj.txt‘,‘r‘,encoding=‘utf-8‘)
characers=[]
stat={}
for line in fr:
line=line.strip()
if len(line)==0:
continue
for x in range(0,len(line)):
if line[x] in [‘ ‘,‘。‘,‘ 。‘,‘“‘,"”","{","}","(",")","[","]","<<",">>","?",",",":",":","!","?"]:
continue
if line[x] not in characers:
characers.append(line[x])
if line[x] not in stat.keys():
stat[line[x]]=0
stat[line[x]]+=1
fw1=open(‘result.json‘,‘w‘)
fw1.write(json.dumps(stat))
fw1.close()
stat=sorted(stat.items(),key=lambda d:d[1],reverse=True ) #d[1]为计数的值
print(type(stat),len(stat))

fw=open(‘xyjresult.txt‘,‘w‘,encoding="utf8")
for item11 in stat:
fw.write(item11[0]+","+str(item11[1])+"\n")
fr.close()
fw.close()

对一个文本不同的字进行计数

原文:https://www.cnblogs.com/cyh1989/p/9098729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!