Python字符串到日期时间的麻烦

时间:2013-09-22 19:41:33

标签: python datetime

我在Python中有以下字符串 - '2013年10月1日07月30日美国东部时间'。我正在尝试转换为日期时间对象,但收到以下错误:

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')

2 个答案:

答案 0 :(得分:2)

Python strptime与平台有关:

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

  

对%Z指令的支持基于tzname中包含的值以及日光是否为真。因此,它是特定于平台的,除了识别始终已知的UTC和GMT(并且被认为是非夏令时时区)。

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

http://pytz.sourceforge.net/之类的外部库来帮助提供时区

答案 1 :(得分:1)

有两个EST导致混淆:http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

Eastern Standard Time (North America)   UTC-05
Eastern Standard Time (Australia)       UTC+10