json - extract tweets from a text file (python) -


sorry, trying store 'id_str' each tweet new list called ids[].. getting 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 = [] line in open (sys.argv[1]): try:   tweets.append(json.loads(line)) except:   pass ids = [] tweet in tweets: ids.append(tweet['id_str']) 

the json data tweets missing fields. try this,

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

or equivalently,

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

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -