【问题标题】:Parsing datetime value using strptime() problem [closed]使用 strptime() 问题解析日期时间值 [关闭]
【发布时间】:2021-07-07 12:56:42
【问题描述】:

我尝试像这样转换数据

'2021-07-06T07:31:02Z'

以这种方式发送给datetime

from datetime import datetime
datetime.strptime(created, '%Y-%m-%dT%H:%M:%S.%fZ')

但出现错误

time data '2021-07-06T07:31:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

我做错了什么?

【问题讨论】:

  • 您的时间字符串中没有.%f...

标签: python python-datetime strptime


【解决方案1】:

您在代码中使用了%f,但在您的字符串示例中没有给出分数。 我尝试了以下方法:

datetime.strptime('2021-07-06T07:31:02Z','%Y-%m-%dT%H:%M:%SZ')
Out[14]: datetime.datetime(2021, 7, 6, 7, 31, 2)

所以要么删除.%f,要么将小数值添加到您的字符串中。

更多信息:

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多