gpt4 book ai didi

Python strptime 值错误 : time data does not match format

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:01 25 4
gpt4 key购买 nike

我已将时区添加到我的 postgreSQL 数据库中的日期时间列。

现在每次我想比较日期时都会出现上面的错误。在某些点上,我有 JSON 请求,日期时间对象作为字符串传递,因此我需要使用我得到的附加时区信息来解析它们:

ValueError: time data '2018-05-02 11:52:26.108540+02:00' 
does not match format '%Y-%m-%d %H:%M:%S.%f+%Z'

之前我有:

2018-05-02 11:52:26.108540

完美配合:

%Y-%m-%d %H:%M:%S.%f

新增的信息是:+02:00

strptime docu它告诉我使用 %z%Z 但它不起作用。

编辑:

我正在使用 Python 3

最佳答案

问题是偏移量 +02:00 您需要删除冒号 ':' 然后它将起作用:

In[48]:
dt.datetime.strptime('2018-05-02 11:52:26.108540+0200', '%Y-%m-%d %H:%M:%S.%f%z')

Out[48]: datetime.datetime(2018, 5, 2, 11, 52, 26, 108540, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))

因此您需要遍历所有日期时间字符串并删除它以便 strptime 正确解析它

关于Python strptime 值错误 : time data does not match format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com