Created
February 2, 2012 13:48
-
-
Save enagorny/1723541 to your computer and use it in GitHub Desktop.
converts western digits to arabic equivalent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def translate_to_ar_numbers(num): | |
| """ | |
| Translates western digits in string to their correspondings arabic equivalent | |
| Args: | |
| num: number or string with numbers where we need to replace digits | |
| Returns: | |
| unicode string with replaced to arabic digits | |
| for example: u'\xd9\xd9\xd9-\xd9\xd9\xd9' -- for num="220-240" | |
| u'\xa3\xa2\xd9.\xd9\xa2' -- for num="750.45" | |
| """ | |
| # 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'\xd9', 49: u'\xa0', 50: u'\xd9', 51: u'\xa1', 52: u'\xd9', | |
| 53: u'\xa2', 54: u'\xd9', 55: u'\xa3', 56: u'\xd9', 57: u'\xa4'} | |
| return unicode(num).translate(ttable) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment