with open(textinfo,'rb') as fp:
data=fp.read()
text=unicode(data,"gbk")
cnt=Counter()
for word in text:
cnt[word]+=1
cnt=dict(cnt)
L=sorted(cnt.items(),key = lambda x:x[1],reverse = True) #排序
L = L[:20] #前20个
dictdata = {}
for l in L:
print l[0],l[1]
本文介绍了一种使用Python读取文本文件并进行字符频率分析的方法。通过将文件内容转换为Unicode格式,利用Counter计数器统计每个字符出现的次数,并按频率排序,最后输出前20个最频繁出现的字符及其频率。
162





