Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Arabic Characters (Eastern Arabic Numerals) to Arabic Numerals in Python

Tags:

python

unicode

Some of our clients submit timestamps like ٢٠١٥-١٠-٠٣ ١٩:٠١:٤٣ which Google translates to "03/10/2015 19:01:43". Link here.

How can I achieve the same in Python?

like image 899
kev Avatar asked Oct 22 '25 15:10

kev


2 Answers

There is also the unidecode library from https://pypi.python.org/pypi/Unidecode.

In Python 2:

>>> from unidecode import unidecode
>>> unidecode(u"۰۱۲۳۴۵۶۷۸۹")
'0123456789'

In Python 3:

>>> from unidecode import unidecode
>>> unidecode("۰۱۲۳۴۵۶۷۸۹")
'0123456789'
like image 170
phk Avatar answered Oct 24 '25 05:10

phk


To convert the time string to a datetime object (Python 3):

>>> import re
>>> from datetime import datetime
>>> datetime(*map(int, re.findall(r'\d+', ' ٢٠١٥-١٠-٠٣ ١٩:٠١:٤٣')))
datetime.datetime(2015, 10, 3, 19, 1, 43)
>>> str(_)
'2015-10-03 19:01:43'

If you need only numbers:

>>> list(map(int, re.findall(r'\d+', ' ٢٠١٥-١٠-٠٣ ١٩:٠١:٤٣')))
[2015, 10, 3, 19, 1, 43]
like image 43
jfs Avatar answered Oct 24 '25 06:10

jfs