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 the dateutil
parser:
import locale
from dateutil.parser import parse as parse_dt
locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8') ## first I set locale
## locale.LC_TIME, 'fr_FR.UTF-8')
parse_dt('3 juillet',fuzzy= True) ## don't work give the default month
## Out[29]: datetime.datetime(2014, 10, 3, 0, 0)
parse_dt(u'4 Août ',fuzzy= True) ## same thing using another month
I am parsing dates without know in advance the format of my string. The idea is to parse many dates in fly :
parse_dt(u'Aujourd''hui ',fuzzy= True)
parse_dt(u'Hier',fuzzy= True)
Using parsedatime library and some regular expression to translate french words , I can get this:
import parsedatetime
import re
cal = parsedatetime.Calendar()
cal.parse(re.sub('juil.*' ,'jul' ,'20 juillet'))
((2015, 7, 20, 10, 25, 47, 4, 283, 1), 1)
Maybe should I generalize this to all french months?
Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.
The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.
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
.
2015-09-09
2015-07-03
2015-08-04
2015-09-08
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With