时间数据与格式不匹配 [英] time data does not match format

查看:182
本文介绍了时间数据与格式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

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

但是我看不到%d /%m /%Y%H:%M:%S.%f 吗?

这是我使用的代码。

from datetime import datetime
time_value = datetime.strptime(csv_line[0] + '000', '%d/%m/%Y %H:%M:%S.%f')

我已经添加并删除了 000 ,但是我遇到了同样的错误。

I have added and removed the 000 but I get the same error.

推荐答案

您将月份和日期交换了:

You have the month and day swapped:

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

<$ c否则,$ c> 28 永远不会适合%m month参数的范围。

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

使用%m %d 正确的顺序解析工作:

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)

您无需添加'000'; %f 可以正确解析较短的数字:

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)

这篇关于时间数据与格式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆