给定正确的格式,strptime 无法解析

strptime failing to parse given a correct format

我已经记下了 strptime 应该用来将我的字符串解析为日期时间的格式,但它一直失败,但没有明确的原因。

我有一个要解析的 date/time 字符串:'2019-06-17T05:35:30' 我给了它以下格式:'%y-%m-%dT%H:%M:%S' 显然不匹配。

我尝试使用替换方法将 'T' 替换为 space,并相应地更改格式,但无济于事。

tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19] #Excluded 
#milliseconds by trimming whatever is after the '.', including the '.' itself.
dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

----------------Jupyter错误显示--------------------

ValueError                                Traceback (most recent call last)
<ipython-input-13-a379ef67d5ba> in <module>
     20 tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19]
     21 #tmpTime = tmpTime.replace("T"," ")
---> 22 dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

~\AppData\Local\Programs\Python\Python37\lib\_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,)

~\AppData\Local\Programs\Python\Python37\lib\_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 '2019-06-17T05:35:30' does not match format '%y-%m-%dT%H:%M:%S'

Supposed to parse correctly into a datetime object, fails.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-a379ef67d5ba> in <module>
     20 tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19]
     21 #tmpTime = tmpTime.replace("T"," ")
---> 22 dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

~\AppData\Local\Programs\Python\Python37\lib\_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,)

~\AppData\Local\Programs\Python\Python37\lib\_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 '2019-06-17T05:35:30' does not match format '%y-%m-%dT%H:%M:%S'

您的字符串格式中年份的大小写错误。这应该表示为“%Y”。例如:

tmpTime =  '2019-06-17T05:35:30'
dt = datetime.strptime(tmpTime, '%Y-%m-%dT%H:%M:%S')
>>>dt
datetime.datetime(2019, 6, 17, 5, 35, 30)

您可以找到正确的日期时间解析格式here