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

时间:2019-06-24 07:45:46

标签: python python-3.x strptime

我已经写下了strptime用来将我的字符串解析为datetime的格式,但是在没有明确原因的情况下它一直失败。

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

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

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'

1 个答案:

答案 0 :(得分:1)

年份的字符串格式错误,应将其表示为'%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