Python 3.6 DateTime Strptime返回错误,而Python 3.7正常运行 [英] Python 3.6 DateTime Strptime Returns error while Python 3.7 works well

查看:317
本文介绍了Python 3.6 DateTime Strptime返回错误,而Python 3.7正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为日期数据创建了一种数据类型,该数据类型返回了 datetime.datetime 对象

I just created a data type for my date data, which returns a datetime.datetime object

此处代码是:

import datetime


class Date:
    def __new__(cls, dateTime, *args, **kwargs):
        return datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%f%z")

因此,每次我给这个班级 ISO-8601 ,它应该从字符串中返回datetime对象...

So everytime I give this class an ISO-8601 it should return the datetime object from the string...

Python 3.7示例:

Python 3.7 Example:

Date("2018-12-09T08:56:12.189Z")                                        
# Returns => datetime.datetime(2018, 12, 9, 8, 56, 12, 189000, tzinfo=datetime.timezone.utc)

这很好用,但是当我在Python 3.6或Python 3.5上使用它时:

This works damn well, but when I use it on Python 3.6 or Python 3.5:

# Python 3.5 Traceback
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Python 3.6 Traceback
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

太奇怪了,是什么原因引起的?我该如何解决?

It's so weird, What causes the problem? How can I fix it?

推荐答案

好吧,两天后,我检查了 Python 3.7更改日志,我发现支持 Z 作为UTC在Python 3.7中添加了offset。请参阅Python问题跟踪器上的 issue ,该问题主要是关于添加对冒号的支持,但也提到 Z 支持在页面的下方。另请参见 datetime 文档,其中说

Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say


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

Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.

在我的课程中,我必须将时间格式更改为此:

On my class, I had to change the time format to this:

datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")

我更改了%z 最后到 Z ,对偏移量进行硬编码。

I changed the %z at the end to Z, hardcoding the offset.

这篇关于Python 3.6 DateTime Strptime返回错误,而Python 3.7正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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