Python3 以 2018-01-14T23:55:27.337Z 格式解析日期时间
Python3 parsing datetime in format 2018-01-14T23:55:27.337Z
我尝试了以下几种扰动:
datetime.strptime('2018-01-14T23:55:27.337Z',"%Y-%m-%dT%H:%M:%S.%3N%Z")
但得到这样的错误:
ValueError Traceback (most recent call last)
<ipython-input-45-babd38d0d73f> in <module>()----> 1 datetime.strptime('2018-01-14T23:55:27.337Z',"%Y-%m-%dT%H:%M:%S.%3N%Z")
/usr/lib/python3.5/_strptime.py in _strptime_datetime(cls, data_string, format)
508 """Return a class cls instance based on the input string and the
509 format string."""
--> 510 tt, fraction = _strptime(data_string, format)
511 tzname, gmtoff = tt[-2:]
512 args = tt[:6] + (fraction,)
/usr/lib/python3.5/_strptime.py in _strptime(data_string, format)
333 del err
334 raise ValueError("'%s' is a bad directive in format '%s'" %
--> 335 (bad_directive, format)) from None
336 # IndexError only occurs when the format string is "%"
337 except IndexError:
ValueError: '3' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%3N%Z'
尝试这样做。
datetime.strptime('2018-01-14T23:55:27.337Z', '%Y-%m-%dT%H:%M:%S.%fZ')
注意第二个参数。是 '%Y-%m-%dT%H:%M:%S.%fZ'
而不是 '%Y-%m-%dT%H:%M:%S.%3N%Z'
。您收到错误的指令值错误,因为 %3
不是指令。
如果您需要,here's strptime()
的文档。
我尝试了以下几种扰动:
datetime.strptime('2018-01-14T23:55:27.337Z',"%Y-%m-%dT%H:%M:%S.%3N%Z")
但得到这样的错误:
ValueError Traceback (most recent call last)
<ipython-input-45-babd38d0d73f> in <module>()----> 1 datetime.strptime('2018-01-14T23:55:27.337Z',"%Y-%m-%dT%H:%M:%S.%3N%Z")
/usr/lib/python3.5/_strptime.py in _strptime_datetime(cls, data_string, format)
508 """Return a class cls instance based on the input string and the
509 format string."""
--> 510 tt, fraction = _strptime(data_string, format)
511 tzname, gmtoff = tt[-2:]
512 args = tt[:6] + (fraction,)
/usr/lib/python3.5/_strptime.py in _strptime(data_string, format)
333 del err
334 raise ValueError("'%s' is a bad directive in format '%s'" %
--> 335 (bad_directive, format)) from None
336 # IndexError only occurs when the format string is "%"
337 except IndexError:
ValueError: '3' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%3N%Z'
尝试这样做。
datetime.strptime('2018-01-14T23:55:27.337Z', '%Y-%m-%dT%H:%M:%S.%fZ')
注意第二个参数。是 '%Y-%m-%dT%H:%M:%S.%fZ'
而不是 '%Y-%m-%dT%H:%M:%S.%3N%Z'
。您收到错误的指令值错误,因为 %3
不是指令。
如果您需要,here's strptime()
的文档。