Python 中的解析器日期
Parser date in Python
我有一个日期列表。一些日期使用 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"
不要删除那么多信息。保留 :
冒号(您的正则表达式删除了它们,但我敢打赌在您破坏它们之前就已经存在);如果有时间分隔符,你的日期解析得很好:
>>> 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/Whosebug-2.7/lib/python2.7/site-packages/dateutil/parser.py", line 743, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/Users/mpieters/Development/venvs/Whosebug-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())
我有一个日期列表。一些日期使用 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"
不要删除那么多信息。保留 :
冒号(您的正则表达式删除了它们,但我敢打赌在您破坏它们之前就已经存在);如果有时间分隔符,你的日期解析得很好:
>>> 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/Whosebug-2.7/lib/python2.7/site-packages/dateutil/parser.py", line 743, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/Users/mpieters/Development/venvs/Whosebug-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())