我想遍历一个文本文件并打印unicode的出现。在值域的位置,我想要一个合适的函数。
counter = {}
x={'0x0985':1,'0x0986':2,'0x0987':3,'0x0988':4,'0x0989':5}
for key,value in x.items():
x[key]=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,0x0986,0x0987,0x0988,0x0989}
counter[n] += 1
for key, value in counter.items():
print(chr(key), value)
发布于 2018-02-27 21:20:05
我完全不明白你为什么要在这里使用range。
if n in (0x0985,0x0986,0x0987,0x0988,0x0989):
发布于 2018-02-27 21:43:46
if n in range(2437,2442): #is enough
或
if n in range(0x985,0x98a):
或
my_unicode=(0x0985,0x0986,0x0987,0x0988,0x0989,0x98A,0x098B,0x098F)
if n in my_unicode:pass
https://stackoverflow.com/questions/49009656
复制相似问题