Parse French date in python

后端 未结 3 887
耶瑟儿~
耶瑟儿~ 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:08
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import parsedatetime as pdt # $ pip install parsedatetime pyicu
    
    calendar = pdt.Calendar(pdt.Constants(localeID='fr', usePyICU=True))
    for date_string in [u"Aujourd'hui", "3 juillet", u"4 Août", u"Hier"]:
        dt, success = calendar.parseDT(date_string)
        if success:
           print(date_string, dt.date())
    

    Output

    3 juillet 2015-07-03
    4 Août 2015-08-04
    

    Aujourd'hui, Hier are not recognized (parsedatetime 1.4).

    The current version on github (future 1.5) supports customizing the day offsets. It can be used to parse Aujourd'hui, Hier:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import parsedatetime as pdt
    
    class pdtLocale_fr(pdt.pdt_locales.pdtLocale_icu):
        def __init__(self):
            super(pdtLocale_fr, self).__init__(localeID='fr_FR')
            self.dayOffsets.update({u"aujourd'hui": 0, u'demain': 1, u'hier': -1})
    
    pdt.pdtLocales['fr_FR'] = pdtLocale_fr
    
    calendar = pdt.Calendar(pdt.Constants(localeID='fr_FR', usePyICU=False))
    for date_string in [u"Aujourd'hui", "3 juillet", u"4 Août", u"Hier",
                        u"au jour de hui", u"aujour-d’hui",
                        u"au-jour-d’hui", "demain", "hier",
                        u"today", "tomorrow", "yesterday"]:
        dt, rc = calendar.parseDT(date_string)
        if rc > 0:
           print(date_string, dt.date())
    

    latest version

    Output

    Aujourd'hui 2014-10-11
    3 juillet 2015-07-03
    4 Août 2015-08-04
    Hier 2014-10-10
    demain 2014-10-12
    hier 2014-10-10
    today 2014-10-11
    tomorrow 2014-10-12
    yesterday 2014-10-10
    

    To install it, run:

    $ pip install git+https://github.com/bear/parsedatetime
    
    0 讨论(0)
  • 2020-12-03 18:11

    dateparser module can parse dates in the question:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import dateparser # $ pip install dateparser
    
    for date_string in [u"Aujourd'hui", "3 juillet", u"4 Août", u"Hier"]:
        print(dateparser.parse(date_string).date())
    

    It translates dates to English using a simple yaml config and passes the date strings to dateutil.parser.

    Output

    2015-09-09
    2015-07-03
    2015-08-04
    2015-09-08
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题