parsing date string in python (convert string to date)

跟風遠走 提交于 2019-11-30 14:23:47
eumiro

Standard datetime.datetime.strptime has problems with timezone definitions. Use dateutil.parser

>>> from dateutil import parser
>>> parser.parse("2011-10-23T08:00:00-07:00")
datetime.datetime(2011, 10, 23, 8, 0, tzinfo=tzoffset(None, -25200))

If you care about the date part only, you can try it without dateutil.parser:

>>> from datetime import datetime
>>> datetime.strptime(data[4].partition('T')[0], '%Y-%m-%d').date()
datetime.date(2011, 10, 23)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!