如何在python中用-0400时区字符串解析日期? [英] How to parse dates with -0400 timezone string in python?

查看:137
本文介绍了如何在python中用-0400时区字符串解析日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串形式'2009/05/13 19:19:30 -0400'。似乎以前版本的python可能已经在strptime中支持了一个%z格式的标签,用于拖尾时区规范,但是2.6.x似乎已经删除了。

I have a date string of the form '2009/05/13 19:19:30 -0400'. It seems that previous versions of python may have supported a %z format tag in strptime for the trailing timezone specification, but 2.6.x seems to have removed that.

您可以使用dateutil中的解析函数:

What's the right way to parse this string into a datetime object?

推荐答案

b
$ b

You can use the parse function from dateutil:

>>> from dateutil.parser import parse
>>> d = parse('2009/05/13 19:19:30 -0400')
>>> d
datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400))

这样你就可以获得一个可以使用的datetime对象。

This way you obtain a datetime object you can then use.

更新: As 回覆 dateutil2.0是为python3.0编写的,不适用于python2.x。对于python2.x需要使用dateutil1.5。

UPDATE: As answered dateutil2.0 is written for python3.0 and does not work with python2.x. For python2.x dateutil1.5 needs to be used.

这篇关于如何在python中用-0400时区字符串解析日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆