python parsing a datetime with a timezone [duplicate]

梦想与她 提交于 2020-01-10 04:40:07

问题


I am trying to parse an string from a log file in the format: 2011-06-27 10:29:56+0200

If I use datetime.datetime.strptime('%Y-%m-%d %H:%M:%S%z') I get ValueError("'z' is a bad directive in format '%Y-%m-%d %H:%M:%S%z'") thrown

How can I parse a date in this format?


回答1:


Try using dateutil.

from dateutil.parser import parse    
dt = parse("2011-06-27 10:29:56+0200")


来源:https://stackoverflow.com/questions/6571419/python-parsing-a-datetime-with-a-timezone

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