Aravind
Aravind

Reputation: 13

alternative function for range() in python

I want to iterate through a text file and print the occurrence of unicode. In the place of range I want a suitable function.

  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)   

Upvotes: 1

Views: 1606

Answers (2)

Smart Manoj
Smart Manoj

Reputation: 5834

if n in range(2437,2442): #is enough

Or

if n in range(0x985,0x98a):

Or

my_unicode=(0x0985,0x0986,0x0987,0x0988,0x0989,0x98A,0x098B,0x098F)
if n in my_unicode:pass

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

I don't see why you want to use range here at all.

if n in (0x0985,0x0986,0x0987,0x0988,0x0989):

Upvotes: 2

Related Questions