如何转换“ 2014年6月5日星期四10:59:10 CDT”到python datetime对象? [英] How to convert "Thu Jun 5 10:59:10 CDT 2014" into python datetime object?
问题描述
如何将 Thu Jun 5 10:59:10 CDT 2014转换为python datetime对象?
How to convert "Thu Jun 5 10:59:10 CDT 2014" into python datetime object?
由于以下原因,我似乎无法正常工作: CDT。 %Z会引发错误:(
I can't seem to get it to work due to the CDT. %Z for that is throwing errors :(
推荐答案
您不能使用 datetime
在此处构造对象。如文件中提到的, strptime
仅可与(空),UTC,EST,CST
一起使用:
You can't use usual methods from datetime
to construct the object here. As mentioned in the documentation, strptime
can work only with (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'
Yo您需要看看 python-dateutil
,该对象会将对象解析为 naive 日期时间对象(不考虑时区...):
You need to take a look at python-dateutil
, which will parse the object into a naive datetime object (it does not take into account the timezone...):
>>> from dateutil import parser
>>> parser.parse("Thu Jun 5 10:59:10 CDT 2014")
datetime.datetime(2014, 6, 5, 10, 59, 10)
这篇关于如何转换“ 2014年6月5日星期四10:59:10 CDT”到python datetime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!