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
Edit : add some context:
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)
Edit using another library :
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?
First check whether you have the correct locale in your repo:
If not, do:
Then go back to python:
To use customize date format:
You'll realized that if the year is underspecified it's set to default at
1900
.dateparser
module can parse dates in the question:It translates dates to English using a simple yaml config and passes the date strings to
dateutil.parser
.Output
Output
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:
latest version
Output
To install it, run: