Search code examples
pythonjsontwittertweets

extract tweets from a text file (python)


Sorry, I am just trying to store 'id_str' from each tweet to a new list called ids[].. but getting the following error:

Traceback (most recent call last): File "extract_tweet.py", line 17, in print tweet['id_str'] KeyError: 'id_str'

My code is:

import json
import sys
if __name__ == '__main__':
tweets = []
for line in open (sys.argv[1]):
try:
  tweets.append(json.loads(line))
except:
  pass
ids = []
for tweet in tweets:
ids.append(tweet['id_str'])

Solution

  • The json data from tweets are sometimes missing fields. Try something like this,

    ids = []
    for tweet in tweets:
        if 'id_str' in tweet:
            ids.append(tweet['id_str'])
    

    or equivalently,

    ids = [tweet['id_str'] for tweet in tweets if 'id_str' in tweet]