ISO 到日期时间对象:'z' 是一个错误的指令 [英] ISO to datetime object: 'z' is a bad directive
问题描述
我正在尝试使用以下代码将 ISO 转换为 datetime
:
I am trying to convert ISO to datetime
using the code below:
dt = datetime.datetime.strptime("2013-07-23T15:10:59.342107+01:00",
"%Y-%m-%dT%H:%M:%S.%f%z")
我收到以下错误:
'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z'
将上述格式的 ISO 字符串转换为 datetime
对象的最佳方法是什么?我使用的是 Python 2.7.6 版.
What is the best way to convert an ISO string of above the format to a datetime
object? I'm using Python version 2.7.6.
推荐答案
欢迎使用 Python datetime!处理日期和时间必然很复杂,而且 Python 并没有完全包含在这种情况下的电池.您不能在 strptime
中使用 %z
,因为 Python 没有表示时区的类(您应该实现自己的时区,或者最好包括一些其他库).
Welcome to Python datetime! Dealing with dates and times is necessarily complex, and Python doesn't come fully with batteries included in this case. You can't use %z
in strptime
because Python has no classes to represent timezones (you are supposed to implement your own, or better yet include some other libraries).
您想使用 pytz
和 python-dateutil
.有关更多详细信息,请参阅此处:
You want to use pytz
and python-dateutil
. For more details see here:
这篇关于ISO 到日期时间对象:'z' 是一个错误的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!