我想迭代一个文本文件并打印出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)
答案 0 :(得分:2)
我不明白你为什么要在这里使用范围。
if n in (0x0985,0x0986,0x0987,0x0988,0x0989):
答案 1 :(得分:0)
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