【问题标题】:key error, time data does not match format按键错误,时间数据与格式不匹配
【发布时间】:2023-12-23 06:34:01
【问题描述】:

我收到以下错误:

time data "2020-03-06" doesn't match the format %Y/%m/%d 

但我不明白使用当前参数有什么错误 我正在使用以下代码

tmp = dt.datetime.strptime(date[i], '%Y/%m/%d')

【问题讨论】:

  • 从错误中可以清楚地看出您使用了错误的日期格式,请检查列值。

标签: python time helper


【解决方案1】:

由于错误消息表明格式需要“%Y/%m/%d”,因此您有“2020-03-06” 用反斜杠替换破折号。 即

tmp = dt.datetime.strptime("2020/03/06", '%Y/%m/%d')

这应该可以解决错误。 由于日期看起来像在 date[i] 中存储为字符串,你可以这样做 date[i].replace("-","/")

【讨论】: