Python中的解析器日期

时间:2015-02-10 11:31:55

标签: python python-datetime date-parsing python-dateutil

我有一份日期清单。一些日期使用from dateutil import parser进行解析,但其他日期不会。未解析的日期列表如下: -

date1 = 'Tue Feb 10  2015 12 52pm IST'
date2 = '10 February  15  08 35am'
date3 = '2015 02 10 08 24 26 UTC'

我按以下方式解析日期: -

try:
    date = re.sub('[^a-zA-Z0-9\n\.]', ' ', date)
    print date
    print (parser.parse(date)).date()
except Exception,e:
    print e

如何解析所有日期格式?这些是从网页上删除的日期。

最终输出的格式应为"Monday, 09 Feb"

1 个答案:

答案 0 :(得分:1)

不要删除这么多信息。留在:冒号(你的正则表达式删除它们,但我打赌在你破坏它们之前有那里);如果有时间分隔符,您的日期就会解析:

>>> from dateutil.parser import parse
>>> date1 = 'Tue Feb 10  2015 12 52pm IST'
>>> parse(date1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/dateutil/parser.py", line 743, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/mpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/dateutil/parser.py", line 310, in parse
    ret = default.replace(**repl)
ValueError: hour must be in 0..23
>>> date1_with_colon = 'Tue Feb 10  2015 12:52pm IST'
>>> parse(date1_with_colon)
datetime.datetime(2015, 2, 10, 12, 52)
>>> date2_with_colon = '10 February  15  08:35am'
>>> parse(date2_with_colon)
datetime.datetime(2015, 2, 10, 8, 35)
>>> date3_with_colon = '2015 02 10 08:24:26 UTC'
>>> parse(date3_with_colon)
datetime.datetime(2015, 2, 10, 8, 24, 26, tzinfo=tzutc())