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

时间:2023-03-09 03:19:08
bash python获取文本中每个字符出现的次数

bash:

grep -o . myfile | sort |uniq -c

python:  使用collections模块

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)