2013-09-22 23 views
1

我在Python中有以下字符串 - 'Oct 01 2013 07 30 PM EST'。我试图转换为日期时间对象,但出现以下错误:Python字符串到日期时间的麻烦

ValueError: time data 'Oct 01 2013 07 30 PM EST' does not match format 
'%b %d %Y %I %M %p %Z' 

不知道我要去哪里错了!请帮忙。我的代码如下:

from datetime import datetime 
date_object = datetime.strptime(str(date), '%b %d %Y %I %M %p %Z') 
+0

的原因是时区。请检查这个问题http://stackoverflow.com/questions/1703546/parsing-date-time-string-with-timezone-abbreviated-name-in-python – oleg

+0

啊,谢谢!我刚刚删除了时区,现在它可以正常工作。 – sewardth

回答

2

Python的strptime是依赖于平台的:

http://docs.python.org/2/library/time.html#time.strptime

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

在我的包(OSX 10.8.5,蟒蛇2.7.2),例如,datetime.now().strftime("%Z")是一个空字符串。

有外部库就像http://pytz.sourceforge.net/帮助时区