如何将清华君5 10:59:10 CDT 2014转换为python datetime对象?
由于CDT的原因,我好像不能让它工作。%Z用于抛出错误:(
发布于 2014-06-06 00:11:44
在这里,您不能使用datetime
中的常用方法来构造对象。作为mentioned in the documentation,strptime
只能与(empty), UTC, EST, CST
一起使用
>>> datetime.datetime.strptime("Thu Jun 05 10:59:10 UTC 2014", "%a %b %d %H:%M:%S %Z %Y")
datetime.datetime(2014, 6, 5, 10, 59, 10)
>>> datetime.datetime.strptime("Thu Jun 05 10:59:10 CDT 2014", "%a %b %d %H:%M:%S %Z %Y")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'Thu Jun 05 10:59:10 CDT 2014' does not match format '%a %b %d %H:%M:%S %Z %Y'
您需要take a look at python-dateutil
,这将把对象解析成一个朴素的datetime对象(它不考虑时区...):
>>> from dateutil import parser
>>> parser.parse("Thu Jun 5 10:59:10 CDT 2014")
datetime.datetime(2014, 6, 5, 10, 59, 10)
发布于 2014-06-06 00:05:32
dateutil
可以轻松地解析大多数日期格式
$ easy_install dateutil
>>> from dateutil import parser
>>> parser.parse("Thu Jun 5 10:59:10 CDT 2014")
datetime.datetime(2014, 6, 5, 10, 59, 10)
(我认为它不能很好地处理时区问题……但它肯定会解析它。)
https://stackoverflow.com/questions/24064906
复制相似问题