我使用python-twitter获取tweet的日期,并尝试用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')这抛出了上面提到的异常。
它在交互式shell上工作:
>>> 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编码。
发布于 2009-08-12 09:56:15
可以尝试的内容:
(1)有没有可能你的互动会话和你的"bash“使用不同的语言环境?将print time.strftime(some known struct_time)放入您的脚本中,看看日和月是否以不同的语言出现。
(2)将print repr(date)放在脚本中,以明确显示您从latest.GetCreatedAt()调用中获得了什么。
https://stackoverflow.com/questions/1265064
复制相似问题