【问题标题】:ISO8601 format support in python datetime modulepython datetime 模块中的 ISO8601 格式支持
【发布时间】:2018-10-18 23:40:46
【问题描述】:

我已经阅读了很多关于在 python 中解析 ISO8601 的问题,但其中大多数都使用外部依赖项。 然后我碰到了这个问题,

How to parse an ISO 8601-formatted date?

说明python不支持iso8601格式,但答案是3岁。

我需要在不使用任何外部依赖项的情况下解析此日期,

from datetime import datetime
app_login = "1996-12-19T16:39:57+08:00"
parse_app_login = datetime.strptime(x,"%Y-%m-%dT%H:%M:%S%z")
print(parse_app_login)

我得到错误:

ValueError: time data '1996-12-19T16:39:57+08:00' does not match format '%Y-%m-%dT%H:%M:%S%z'

我想知道为什么python不支持iso8601格式?

【问题讨论】:

    标签: python datetime iso8601


    【解决方案1】:

    请注意Python 3.7 支持ISO8601 UTC 偏移格式,

    From Docs,

    在 3.7 版中更改: 当 %z 指令提供给 strptime() 方法,UTC 偏移量可以有一个冒号作为分隔符 在小时、分钟和秒之间。例如,“+01:00:00”将是 解析为一小时的偏移量。此外,提供“Z”是 等同于“+00:00”。

    使用 Python 3.7.0(v3.7.0:1bf9cc5093,2018 年 6 月 27 日,04:06:47),

    >>> from datetime import datetime
    >>> x = "2018-10-18T16:39:57+08:00"
    >>> y = datetime.strptime(x,"%Y-%m-%dT%H:%M:%S%z")
    >>> print(y)
    

    输出,

    2018-10-18 16:39:57+08:00
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 2012-08-04
      • 2017-08-22
      相关资源
      最近更新 更多