Python 使用 strptime 解析 - 月日 Hours:Minute:Seconds 年时区 - 格式不匹配
Python parse with strptime - Month Day Hours:Minute:Seconds Year Timezone - Does not match format
我在使用 python (datetime.datetime.strptime) 解析 "Mar 3 03:00:04 2019 GMT" 时遇到问题。我不知道是什么问题。起初我以为是因为日期没有补零,但是根据文档我发现不是这样。
import datetime
datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
我试过在格式字符串中删除和添加空格。我也试过没有时区说明符但没有运气。我尝试解析的格式来自 ssl 套接字方法 getpeercert
.
ValueError Traceback (most recent call last)
<ipython-input-14-a9f4aa24cc39> in <module>
----> 1 datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
/usr/lib/python3.7/_strptime.py in _strptime_datetime(cls, data_string, format)
575 """Return a class cls instance based on the input string and the
576 format string."""
--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578 tzname, gmtoff = tt[-2:]
579 args = tt[:6] + (fraction,)
/usr/lib/python3.7/_strptime.py in _strptime(data_string, format)
357 if not found:
358 raise ValueError("time data %r does not match format %r" %
--> 359 (data_string, format))
360 if len(data_string) != found.end():
361 raise ValueError("unconverted data remains: %s" %
ValueError: time data '%b %d %H:%M:%S %Y %Z' does not match format 'Mar 3 03:00:04 2019 GMT'
我应该使用的正确格式字符串是什么?
您需要翻转参数,即:
import datetime
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT", "%b %d %H:%M:%S %Y %Z")
datetime.datetime.strptime(date_string, format)
准确的语法应该如上。更改参数的顺序会起作用。
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT","%b %d %H:%M:%S %Y %Z")
datetime.datetime(2019, 3, 3, 3, 0, 4)
我在使用 python (datetime.datetime.strptime) 解析 "Mar 3 03:00:04 2019 GMT" 时遇到问题。我不知道是什么问题。起初我以为是因为日期没有补零,但是根据文档我发现不是这样。
import datetime
datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
我试过在格式字符串中删除和添加空格。我也试过没有时区说明符但没有运气。我尝试解析的格式来自 ssl 套接字方法 getpeercert
.
ValueError Traceback (most recent call last)
<ipython-input-14-a9f4aa24cc39> in <module>
----> 1 datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
/usr/lib/python3.7/_strptime.py in _strptime_datetime(cls, data_string, format)
575 """Return a class cls instance based on the input string and the
576 format string."""
--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578 tzname, gmtoff = tt[-2:]
579 args = tt[:6] + (fraction,)
/usr/lib/python3.7/_strptime.py in _strptime(data_string, format)
357 if not found:
358 raise ValueError("time data %r does not match format %r" %
--> 359 (data_string, format))
360 if len(data_string) != found.end():
361 raise ValueError("unconverted data remains: %s" %
ValueError: time data '%b %d %H:%M:%S %Y %Z' does not match format 'Mar 3 03:00:04 2019 GMT'
我应该使用的正确格式字符串是什么?
您需要翻转参数,即:
import datetime
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT", "%b %d %H:%M:%S %Y %Z")
datetime.datetime.strptime(date_string, format)
准确的语法应该如上。更改参数的顺序会起作用。
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT","%b %d %H:%M:%S %Y %Z")
datetime.datetime(2019, 3, 3, 3, 0, 4)