bash python获取文本中每个字符出现的次数

bash:

grep -o . myfile | sort |uniq -c

python: 使用collections模块

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import pprint
import collections


f = 'xxx'
with open(f) as info:
    count = collections.Counter(info.read().upper())
    value = pprint.pformat(count)
    
    print(value)

import codecs

f = 'xxx'
with codecs.open(f, 'r', 'utf-8') as tf:
    for l in tf:
        for ch in l:
            c[ch] = c.setdefault(ch, 0) + 1

print json.dumps(c, ensure_ascii=False, encoding='utf-8', indent=4)
posted @ 2022-10-07 20:39  I'm_江河湖海  阅读(9)  评论(0)    收藏  举报