Python Date Conversion. How to convert arabic date string into date or datetime object python

前端 未结 5 2003
梦谈多话
梦谈多话 2021-01-23 06:40

I have to convert this date into normal date string/object.

١٩٩٤-٠٤-١١ to 11-04-1994.

5条回答
  •  既然无缘
    2021-01-23 07:19

    It's definitely a Good Idea to use unicodedata.decimal. There's probably a nice way to do this using the locale module and time.strptime / time.strftime, but I don't have any Arabic locales on this machine, so I'm not about to experiment. :)

    FWIW, here's a fairly direct translation of Amadan's JavaScript code into a Python function.

    import re
    
    pat = re.compile(u'[\u0660-\u0669]', re.UNICODE)
    
    def arabic_to_euro_digits(m):
        return unichr(ord(m.group(0)) - 0x630)
    
    def arabic_to_euro_date(arabic_date):
        s = pat.sub(arabic_to_euro_digits, arabic_date)
        return '-'.join(s.split('-')[::-1])
    
    arabic_date = u'١٩٩٤-٠٤-١١'
    print arabic_date
    
    euro_date = arabic_to_euro_date(arabic_date)
    print euro_date
    

    output

    ١٩٩٤-٠٤-١١
    11-04-1994
    

提交回复
热议问题