【发布时间】:2010-11-18 21:37:14
【问题描述】:
我使用python-twitter 获取推文的日期并尝试使用time.strptime() 函数对其进行解析。当我以交互方式进行操作时,一切正常。当我从我的 bash 调用程序时,我得到一个 ValueError 说(例如):
time data u'Wed Aug 12 08:43:35 +0000 2009' does not match
format '%a %b %d %H:%M:%S +0000 %Y'
代码如下:
api = twitter.Api(username='username', password='pw')
user = api.GetUser(username)
latest = user.GetStatus()
date = latest.GetCreatedAt()
date_struct = time.strptime(date, '%a %b %d %H:%M:%S +0000 %Y')
抛出上述异常。
它适用于交互式外壳:
>>> user = api.GetUser('username')
>>> latest = user.GetStatus()
>>> date = latest.GetCreatedAt()
>>> date
u'Wed Aug 12 08:15:10 +0000 2009'
>>>> struct = time.strptime(date, '%a %b %d %H:%M:%S +0000 %Y')
>>>> struct
time.struct_time(tm_year=2009, tm_mon=8, tm_mday=12, tm_hour=8, tm_min=15, tm_sec=10, tm_wday=2, tm_yday=224, tm_isdst=-1)
有人知道为什么会这样吗?
我正在使用 Ubuntu 9.04、Python 2.6.2 和 python-twitter 0.6。 unicode 格式的所有文件。
【问题讨论】:
-
你可能运行了不同版本的python?比较交互式 shell 和 bash shell 的默认解释器的 sys.version 输出。
-
不,交互式 shell 说“2.6.2(release26-maint,2009 年 4 月 19 日,01:56:41)\n[GCC 4.3.3]”并且 Bash 说(python --version) “Python 2.6.2”