我需要将以下格式的日期转换为不同的格式以供显示。但在此之前,我尝试将字符串中的日期转换为时间对象,但无法做到这一点。
>>> time.strptime("Thu Mar 13 23:15:13 2014 EDT", '%a %b %d %H:%M:%S %Y %Z')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib64/python2.4/_strptime.py", line 293, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=Thu Mar 13 23:15:13 2014 EDT fmt=%a %b %d %H:%M:%S %Y %Z
经过反复试验,问题是“%Z”引起的,下面工作正常(只是删除了 %Z)
>>> time.strptime("Thu Mar 13 23:15:13 2014", '%a %b %d %H:%M:%S %Y')
(2014, 3, 13, 23, 15, 13, 3, 72, -1)
Python wiki ( https://docs.python.org/2/library/time.html ) 说时区说明符是 %Z,那么这里有什么问题。请帮我找到。
Python版本:2.4.3
最佳答案
来自 Python 文档。 https://docs.python.org/2/library/time.html#time.strptime
Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).
这基本上是说 time.strptime()
只会识别 time.tzname
中列出的时区
希望这有帮助
关于python - 字符串到日期 - 格式字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835414/