有人可以告诉我如何在Python中解析法语日期吗?抱歉,问题是重复的,但我找不到。
这是我尝试使用dateutil
解析器的内容:
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
我正在解析日期,而不事先知道我的字符串格式。这个想法是在飞行中解析许多日期:
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
模块可以解析问题中的日期:
#!/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())
它使用简单的yaml配置将日期转换为英语,并将日期字符串传递给dateutil.parser
。
2015-09-09
2015-07-03
2015-08-04
2015-09-08