0

Below is the code I am trying to save the results I get from my print status to csv or json

# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
# The Twitter user who we want to get tweets from
name = "mytwitterid"
# Number of tweets to pull
tweetCount = 10

for status in tweepy.Cursor(api.home_timeline).items(10):
    # Process a single status
    print(status.text)

#result = {}

with open('output.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    for row in status.text():
        csvwriter.writerow(row)

This throws error for row in status.text(): TypeError: 'str' object is not callable

6
  • ...as the error message says: status.text (without the brackets) the way you use that a few lines above. Commented Mar 21, 2019 at 17:01
  • Means status.text is str. Just use it as it is Commented Mar 21, 2019 at 17:04
  • Instead of status.text() in your for loop, just use status.text Commented Mar 21, 2019 at 17:08
  • thank you all it works after I remove () but I get blank csv. I don't not see any records Commented Mar 21, 2019 at 17:41
  • @Martin: As it stands, this code should throw an error: status is local to for status in ... therefore check your indentation and show the output of print(status.text). Second, the parameter row must be a sequence, looping a str doesn't result in a sequence. Commented Mar 21, 2019 at 18:02

1 Answer 1

1

You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:

with open('output.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    for status in tweepy.Cursor(api.home_timeline).items(10):
        print(status.text)
        csvwriter.writerow([status.text])

If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.

results = []  # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
    #print(status.text)
    results.append(status.text)

# Now dump results as JSON
with open('results.json', 'w') as outputfile:
    json.dump(results, outputfile)
Sign up to request clarification or add additional context in comments.

5 Comments

works as a charm, how do I remove the spaces and include ',' for each tweet?
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list with append and then just json.dump it after the loop.
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
@Martin See updated answer with a simple JSON example now,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.