在python中解析法语日期 [英] Parse French date in python
问题描述
这是我试过使用 dateutil
解析器:
$ b $来自dateutil.parser的import locale
导入parse as parse_dt
locale.setlocale(locale.LC_TIME,'fr_FR.UTF-8')## first我设置区域设置
## locale.LC_TIME,'fr_FR.UTF-8')
parse_dt('3 juillet',fuzzy = True)##不工作给默认的月份
## out [29]:datetime.datetime(2014,10,3,0,0)
parse_dt(u'4Août',fuzzy = True)##同一件事使用另一个月
编辑:添加一些上下文:
我解析日期不知道提前我的字符串的格式。这个想法是在飞行中解析很多日期:
parse_dt(u'Aujourd''hui',fuzzy = True)
parse_dt(u'Hier',fuzzy = True)
使用另一个库进行编辑:
使用parsedatime库和一些正则表达式来翻译法语,我可以得到这样的:
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)
也许我应该把它推广到所有的法国月份?
解决方案 dateparser
module 可以解析问题中的日期:
#!/ usr / bin / env python
# - * - 编码:utf-8 - * -
import dateparser# $ pip install dateparser
for date_string in [ uAujourd'hui,3 juillet,u4Août,uHier]:
print(dateparser.parse(date_string).date())
它使用 simple yaml config ,并将日期字符串传递给 dateutil.parser
。
输出
2015-09-09
2015-07-03
2015-08-04
2015-09-08
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?
解决方案 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
这篇关于在python中解析法语日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!