Parse French date in python

后端 未结 3 978
耶瑟儿~
耶瑟儿~ 2020-12-03 17:38

Can someone please tell me how can I parse a French date in Python? Sorry if the question is a duplicate but I couldn\'t find one.

Here is what I have tried using t

3条回答
  •  甜味超标
    2020-12-03 18:18

    First check whether you have the correct locale in your repo:

    $ locale -a
    C
    C.UTF-8
    de_AT.utf8
    de_BE.utf8
    de_CH.utf8
    de_DE.utf8
    de_LI.utf8
    de_LU.utf8
    en_AG
    en_AG.utf8
    en_AU.utf8
    en_BW.utf8
    en_CA.utf8
    en_DK.utf8
    en_GB.utf8
    en_HK.utf8
    en_IE.utf8
    en_IN
    en_IN.utf8
    en_NG
    en_NG.utf8
    en_NZ.utf8
    en_PH.utf8
    en_SG.utf8
    en_US.utf8
    en_ZA.utf8
    en_ZM
    en_ZM.utf8
    en_ZW.utf8
    POSIX
    

    If not, do:

    $ sudo locale-gen fr_FR.UTF-8
    Generating locales...
      fr_FR.UTF-8... done
    Generation complete.
    

    Then go back to python:

    $ python
    >>> import locale
    >>> import datetime
    >>> locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
    'fr_FR.UTF-8'
    >>>
    >>> date_txt = "Dimanche 3 Juin 2012"
    >>> DATE_FORMAT = "%A %d %B %Y"
    >>> datetime.datetime.strptime(date_txt, DATE_FORMAT)
    datetime.datetime(2012, 6, 3, 0, 0)
    >>>
    

    To use customize date format:

    >>> date_txt = "3 juillet"
    >>> DATE_FORMAT = "%d %B"
    >>> datetime.datetime.strptime(date_txt, DATE_FORMAT)
    datetime.datetime(1900, 7, 3, 0, 0)
    

    You'll realized that if the year is underspecified it's set to default at 1900.

提交回复
热议问题