Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

parsing - Parse French date in python

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...