tags:

views:

304

answers:

1

I use python-twitter to get the date of a tweet and try to parse it with the time.strptime() function. When I do it interactively, everything works fine. When I call the program from my bash, I get a ValueError saying (for example):

time data u'Wed Aug 12 08:43:35 +0000 2009' does not match 
          format '%a %b %d %H:%M:%S +0000 %Y'

Code looks like this:

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

which throws the exception mentioned above.

It works on the interactive 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)

Someone any idea why this is happening?

I am Using Ubuntu 9.04, Python 2.6.2 and python-twitter 0.6. All files in unicode.

+1  A: 

Things to try:

(1) Is it possible that your interactive session and your "bash" are using different locales? Put print time.strftime(some known struct_time) into your script and see if the day and month come out in a different language.

(2) Put print repr(date) in your script to show unambiguously what you are getting from the latest.GetCreatedAt() call.

John Machin
(1) helped to solve the problem. thanks a lot! test = time.gmtime()print time.strftime('%a %b %d %H:%M:%S +0000 %Y', test)printed it in my locale (de_DE), so for example 'Wed' for Wednesday was 'Mit' for Mittwoch. That caused strptime() to fail.I set locale.setlocale(locale.LC_ALL, 'C'), which is working for the moment. I will have a deeper look into this issue some time later. Thanks again!
dermatthias