Created
March 20, 2012 10:33
-
-
Save enagorny/2133997 to your computer and use it in GitHub Desktop.
converts western digits to arabic equivalent UTF8
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 | |
| 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