bash python獲取文本中每一個字符出現的次數

bash:python

grep -o . myfile | sort |uniq -c

 

python:  使用collections模塊json

import pprint
import collections


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

 或bash

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)
相關文章
相關標籤/搜索