Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尽管格式有效,为什么datetime.datetime.strptime会引发ValueError?_Python_Python 3.x_Datetime_Python3.7 - Fatal编程技术网

Python 尽管格式有效,为什么datetime.datetime.strptime会引发ValueError?

Python 尽管格式有效,为什么datetime.datetime.strptime会引发ValueError?,python,python-3.x,datetime,python3.7,Python,Python 3.x,Datetime,Python3.7,我从数据库中获取这个日期字符串格式,需要将其转换为datetime对象,以便进行减法运算 'from': '2020-06-24T10:12:00.692+00:00' 我尝试过这种格式,但不起作用 "%Y-%m-%dT%H:%M:%S.%f%z" 这个字符串在python中是可转换的吗 更新: 这就是我跑步的时间 datetime_obj1= datetime.strptime(row['from'], "%Y-%m-%dT%H:%M:%S.%f%z"

我从数据库中获取这个日期字符串格式,需要将其转换为datetime对象,以便进行减法运算

'from': '2020-06-24T10:12:00.692+00:00'
我尝试过这种格式,但不起作用

"%Y-%m-%dT%H:%M:%S.%f%z"
这个字符串在python中是可转换的吗

更新: 这就是我跑步的时间

datetime_obj1= datetime.strptime(row['from'], "%Y-%m-%dT%H:%M:%S.%f%z")
我得到的错误

ValueError                                Traceback (most recent call last)
<ipython-input-11-7798f7219c52> in <module>
     23 for row in list_of_events:
     24     print(row['id'])
---> 25     datetime_obj1= datetime.strptime(row['from'], "%Y-%m-%dT%H:%M:%S%z")
     26     print(datetime_obj1)
     27     #print(row['from']-row['to'])

/usr/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    563     """Return a class cls instance based on the input string and the
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]
    567     args = tt[:6] + (fraction,)

/usr/lib/python3.6/_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %

ValueError: time data '2020-06-24T10:12:00.692+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
ValueError回溯(最近一次调用)
在里面
23对于事件列表中的行:
24打印(第['id'行])
--->25 datetime_obj1=datetime.strtime(行['from',%Y-%m-%dT%H:%m:%S%z”)
26打印(日期时间_obj1)
27#打印(第['from']行-第['to']行)
/usr/lib/python3.6//u strptime.py in\u strptime\u datetime(cls,数据字符串,格式)
563“”根据输入字符串和
564格式字符串。”“”
-->565 tt,分数=_strtime(数据字符串,格式)
566 tzname,gmtoff=tt[-2:]
567 args=tt[:6]+(分数,)
/usr/lib/python3.6//u strptime.py in.\u strptime(数据字符串,格式)
360如果未找到:
361 raise VALUERROR(“时间数据%r与格式%r不匹配”%
-->362(数据字符串,格式))
363如果len(数据字符串)!=已找到。结束()
364 raise VALUE ERROR(“未转换的数据剩余:%s”%
ValueError:时间数据“2020-06-24T10:12:00.692+00:00”与格式“%Y-%m-%dT%H:%m:%S%z”不匹配

如果没有一些实际的代码,很难说你的错误在哪里

你建议的日期格式很好用

>>> from datetime import datetime
>>> datetime.strptime("2020-06-24T10:12:00.692+00:00", "%Y-%m-%dT%H:%M:%S.%f%z")
datetime.datetime(2020, 6, 24, 10, 12, 0, 692000, tzinfo=datetime.timezone.utc)

在解析RFC3339日期和时间时出现了一个问题,它在python3.7上得到了解决


因此,
%z
不受

支持。在较旧的Python版本上,您应该能够使用
dateutil
的解析器:

from dateutil import parser

s = '2020-06-24T10:12:00.692+00:00'
dtobj = parser.parse(s)
# dtobj
# datetime.datetime(2020, 6, 24, 10, 12, 0, 692000, tzinfo=tzutc())

在Python3.7+上,使用
datetime.fromisoformat('2020-06-24T10:12:00.692+00:00')
-顺便说一句,你的strtime的格式代码对我来说也很好…
fromisoformat
是:)很好,我还没有意识到。。。我想知道为什么Z字符可以被strtime中的
%Z
解析,而不能被isoformat()中的
解析。。。(另见)