time data does not match format

前端 未结 6 1597
有刺的猬
有刺的猬 2020-12-02 14:24

I get the following error:

time data \'07/28/2014 18:54:55.099000\' does not match format \'%d/%m/%Y %H:%M:%S.%f\'

But I cannot see what pa

6条回答
  •  孤街浪徒
    2020-12-02 14:36

    You have the month and day swapped:

    '%m/%d/%Y %H:%M:%S.%f'
    

    28 will never fit in the range for the %m month parameter otherwise.

    With %m and %d in the correct order parsing works:

    >>> from datetime import datetime
    >>> datetime.strptime('07/28/2014 18:54:55.099000', '%m/%d/%Y %H:%M:%S.%f')
    datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
    

    You don't need to add '000'; %f can parse shorter numbers correctly:

    >>> datetime.strptime('07/28/2014 18:54:55.099', '%m/%d/%Y %H:%M:%S.%f')
    datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
    

提交回复
热议问题