Skip to content

Instantly share code, notes, and snippets.

@enagorny
Created March 20, 2012 10:33
Show Gist options
  • Select an option

  • Save enagorny/2133997 to your computer and use it in GitHub Desktop.

Select an option

Save enagorny/2133997 to your computer and use it in GitHub Desktop.
converts western digits to arabic equivalent UTF8
def translate_to_ar_numbers(num):
"""
Translates western digits in string to their correspondings arabic equivalent
All other characters stays the same
Args:
num: number or string with numbers where we need to replace digits
Returns:
unicode string with replaced to arabic digits
for example: u'٢٢٠-٢٤٠' -- for num="220-240"
"""
# ttable was formed by following code:
# intab = u"0123456789"
# outtab = u"٠١٢٣٤٥٦٧٨٩"
# ttable = dict((ord(from_char), to_char)\
# for from_char, to_char in zip(intab, outtab))
#
# and used here as generated dictionary
# also it was formed without using built-in `make_trans` method
# becouse `make_trans` doesn't work with unicode
ttable = {48: u'٠', 49: u'١', 50: u'٢', 51: u'٣', 52: u'٤',
53: u'٥', 54: u'٦', 55: u'٧', 56: u'٨', 57: u'٩'}
return unicode(num).translate(ttable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment