Question

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'])
Was it helpful?

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]

OTHER TIPS

import json

tweets = []
tweets.append(
        json.loads('{"a": 1}')
)
tweet = tweets[0]
print(tweet)
print( tweet['id_str'] )

--output:--
{'a': 1}

Traceback (most recent call last):
  File "1.py", line 9, in <module>
    print( tweet['id_str'] )
KeyError: 'id_str'

And:

my_dict = {u"id_str": 1}
print my_dict["id_str"]

--output:--
1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top