1

I'm a newbie in programming, but i hope you can help me with my problem. I'm trying to analyse tweets using tweepy/python/stream.api and R (the statistic program).

Right know the stream listener is working, but I can't use the output...

This is the script I'm running:

import tweepy

consumer_key="..."
consumer_secret="..."
access_key = "..."
access_secret = "..."


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print status.text

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['...'])

As a result, I don't get the full tweets (only the first 50 characters), and I can't see the time when it was tweeted. How can i fix this, and is it possible to somehow "print" the output into an Excel file?

4
  • It would probably help if you printed the output you are getting. I only scanned the tweepy website and am still wondering what type of object your sapi is? Are those real keys in your question, if they are should you be displaying them here?
    – PyNEwbie
    Oct 21, 2012 at 23:14
  • Is it okay that you have your consumer_key, consumer_secret, access_key, and access_secret posted for the whole world to see? Oct 21, 2012 at 23:17
  • I suspect he cut them off (hence the missing closing quote), but I removed them anyway because that syntax highlight was driving me mad ^^
    – poke
    Oct 21, 2012 at 23:30
  • thats what the output looks like for sapi.filter(track=['obama']) >>> RT @KendrickLaamar: My Time Line When the Debate is on = Obama, Obama, Obama, F**k Romney, Obama, Obama, & some hoe talking bout she ... (it just ends with the three dots) Oct 22, 2012 at 0:40

2 Answers 2

0

Write the output into .csv file or use the xlrd package. As far as the 50 characters is concerned I don't know. Looks like this has to do with the library.

2
  • thanks for the fast answer! but isn't the xlrd only for excel to python? and are the csv files automatically created with date and time? Oct 22, 2012 at 0:36
  • you're right xlrd is read only. Usually every file has a timestamp if that is what you mean. I'm not quite sure. Otherwise you can always use datetime (strftime) to output what you need.
    – RParadox
    Oct 22, 2012 at 0:39
0

Change your print status.text to make use of xlwt to write directly to a cell in an excel sheet. I've hacked about with it and it's OK, but your code tends to end up quite verbose.

http://pypi.python.org/pypi/xlwt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.