【发布时间】:2019-09-26 14:30:20
【问题描述】:
我想使用 python 的 dateutil 包从 string 中提取 date。 date 有不同的格式,但date 的day 部分在这些string 中的任何一个中都不存在。
Month 以字母形式写在year 之前,如Sep 2016,但在写为数字时,year 成功,如2016-09 或201609
import dateutil.parser as dparser
print(dparser.parse("The file is for month Sep 2016.",fuzzy=True).month)
9
print(dparser.parse("The file is for month Sept-2016.",fuzzy=True).month)
9
print(dparser.parse("The file is for month 2016-09.",fuzzy=True).month)
9
如下图year和month之间没有连字符-时如何处理-
print(dparser.parse("The file is for month 201609.",fuzzy=True).month)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-123-566e083c8313> in <module>()
----> 1 dparser.parse("The file is for month 201609.",fuzzy=True).month
~\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser.py in parse(timestr, parserinfo, **kwargs)
1180 return parser(parserinfo).parse(timestr, **kwargs)
1181 else:
-> 1182 return DEFAULTPARSER.parse(timestr, **kwargs)
1183
1184
这个库中是否有这样做的选项?
【问题讨论】:
标签: python date python-dateutil