How to convert Python's .isoformat() string back into datetime object [duplicate]

狂风中的少年 提交于 2019-12-29 03:12:13

问题


So in Python 3, you can generate an ISO 8601 date with .isoformat(), but you can't convert a string created by isoformat() back into a datetime object because Python's own datetime directives don't match properly. That is, %z = 0500 instead of 05:00 (which is produced by .isoformat()).

For example:

>>> strDate = d.isoformat()
>>> strDate
'2015-02-04T20:55:08.914461+00:00'

>>> objDate = datetime.strptime(strDate,"%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\Lib\_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "C:\Python34\Lib\_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data '2015-02-04T20:55:08.914461+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

From Python's strptime documentation: (https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior)

%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030

So, in short, Python does not even adhere to its own string formatting directives.

I know datetime is already terrible in Python, but this really goes beyond unreasonable into the land of plain stupidity.

Tell me this isn't true.


回答1:


As it turns out, this is the current best "solution" to this question:

pip install python-dateutil

Then...

import datetime
import dateutil.parser

def getDateTimeFromISO8601String(s):
    d = dateutil.parser.parse(s)
    return d



回答2:


Try this:

>>> def gt(dt_str):
...     dt, _, us= dt_str.partition(".")
...     dt= datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
...     us= int(us.rstrip("Z"), 10)
...     return dt + datetime.timedelta(microseconds=us)

Usage:

>>> gt("2008-08-12T12:20:30.656234Z")
datetime.datetime(2008, 8, 12, 12, 20, 30, 656234)


来源:https://stackoverflow.com/questions/28331512/how-to-convert-pythons-isoformat-string-back-into-datetime-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!