a gift function and a question

  • Thread starter Mohsen Pahlevanzadeh
  • Start date
M

Mohsen Pahlevanzadeh

Dear all,

I have a gift for mailing list:

////////////////////////////////
def integerToPersian(number):
listedPersian = ['Û°','Û±','Û²','Û³','Û´','Ûµ','Û¶','Û·','Û¸','Û¹']
listedEnglish = ['0','1','2','3','4','5','6','7','8','9']
returnList = list()

listedTmpString = list(str(number))

for i in listedTmpString:
returnList.append(listedPersian[listedEnglish.index(i)])

return ''.join(returnList)
////////////////////////////////////
When you call it such as : "integerToPersian(3455)" , it return Û³Û´ÛµÛµ,
Û³Û´ÛµÛµ is equivalent to 3455 in Persian and Arabic language.When you read
a number such as reading from databae, and want to show in widget, this
function is very useful.

My question is , do you have reverse of this function? persianToInteger?

Yours,
Mohsen
 
S

Steven D'Aprano

Dear all,

I have a gift for mailing list:

////////////////////////////////
def integerToPersian(number):
listedPersian = ['Û°','Û±','Û²','Û³','Û´','Ûµ','Û¶','Û·','Û¸','Û¹']
listedEnglish = ['0','1','2','3','4','5','6','7','8','9']
returnList = list()
listedTmpString = list(str(number))
for i in listedTmpString:
returnList.append(listedPersian[listedEnglish.index(i)])
return ''.join(returnList)
////////////////////////////////////
When you call it such as : "integerToPersian(3455)" , it return Û³Û´ÛµÛµ,
Û³Û´ÛµÛµ is equivalent to 3455 in Persian and Arabic language.When you read
a number such as reading from databae, and want to show in widget, this
function is very useful.


Thank you Mohsen!


Here is a slightly more idiomatic version of the same function. This is
written for Python 3:

def integerToPersian(number):
"""Convert positive integers to Persian.
'Û³Û´ÛµÛµ'

Does not support negative numbers.
"""
digit_map = dict(zip('0123456789', 'Û°Û±Û²Û³Û´ÛµÛ¶Û·Û¸Û¹'))
digits = [digit_map[c] for c in str(number)]
return ''.join(digits)


Python 2 version will be nearly the same except it needs to use the u
prefix on the strings.


My question is , do you have reverse of this function? persianToInteger?

The Python built-in int function already supports that:


py> int('Û³Û´ÛµÛµ')
3455
 
R

random832

def integerToPersian(number):
"""Convert positive integers to Persian.
'Û³Û´ÛµÛµ'

Does not support negative numbers.
"""
digit_map = dict(zip('0123456789', 'Û°Û±Û²Û³Û´ÛµÛ¶Û·Û¸Û¹'))
digits = [digit_map[c] for c in str(number)]
return ''.join(digits)

You can support negative numbers if you use digit_map.get(c,c).
 
S

Steven D'Aprano

The Python built-in int function already supports that:


py> int('Û³Û´ÛµÛµ')
3455


Oh, I forgot to mention, this works back to at least Python 2.4, provided
you remember to use Unicode strings rather than bytes:


py> b = 'Û³Û´ÛµÛµ' # This fails.
py> int(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\xdb\xb3\xdb\xb4\xdb
\xb5\xdb\xb5'
py>
py> s = u'Û³Û´ÛµÛµ' # This works.
py> int(s)
3455
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top