在python中,我如何遍历文本文件并计算unicodes的出现次数?因为我对python很新。
with open ('bengali.txt') as infile:
for infile in range(0x0985,0x0989):
print unichr(infile)
这是正确的代码??
答案 0 :(得分:0)
如果您只想查看range(0x0985,0x0989)
,可以执行以下操作:
count = 0
with open('bengali.txt', 'r', encoding='utf-8') as infile:
for line in infile:
for char in line:
if ord(char) in range(0x0985,0x0989):
count += 1
编辑:打印字符:
for x in range(0x0985,0x0989):
print(chr(x))
编辑2:计算字符:
counter = {}
for x in range(0x0985,0x0989):
counter[x] = 0
with open('bengali.txt', 'r', encoding='utf-8') as infile:
for line in infile:
for char in line:
n = ord(char)
if n in range(0x0985,0x0989):
counter[n] += 1
for key, value in counter.items():
print(chr(key), value)