Convert unicode to datetime proper strptime format

匿名 (未验证) 提交于 2019-12-03 02:18:01

问题:

I am trying to convert a unicode object to a datetime object.

I read through the documentation: http://docs.python.org/2/library/time.html#time.strptime

and tried

datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%SZ') 

but I get the error message ValueError: time data '2014-01-15T01:35:30.314Z' does not match format '%Y-%m-%dT%H:%M:%SZ'

Any feedback on what is the proper format?

I appreciate the time and expertise.

回答1:

You can parse the microseconds:

from datetime import datetime date_posted = '2014-01-15T01:35:30.314Z' datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%S.%fZ')


回答2:

One option is to let dateutil do the job:

>>> from dateutil import parser >>> parser.parse('2014-01-15T01:35:30.314Z') datetime.datetime(2014, 1, 15, 1, 35, 30, 314000, tzinfo=tzutc())


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