Skip to content Skip to sidebar Skip to footer

Parse Json To Csv Twitter Data Keyerror: 'user'

I am trying to parse some tweets data I collected in a JSON file right now. The problem is some of the tweets don't have 'user' or 'place' in them. As a result, I get messages like

Solution 1:

Use dict.get.

if tweet.get('user'):
                tweets_location.append(tweet['user'].get('location', ''))
        else:
                tweets_location.append("")

See Why dict.get(key) instead of dict[key]?

Solution 2:

You are getting a KeyError. If you want to check whether the key is in the dictionary, do:

if'user' in tweet:
    tweets_location.append( tweet['user']['location'] )

Or you could embed it in a try..except:

try:
    tweets_location.append( tweet['user']['location'] )
except KeyError:
    tweets_location.append('')

Alternatively, you may use the get method of dict, as suggested by XrXrXr. The get method gives you a convenient way of providing a default value, i.e., so you can do it all in one line:

tweets_location.append( tweet.get('user', '').get('location', '') )

This defaults to the empty string if 'user' is not a key in tweet, and also to the empty string if the 'location' is not a key of tweet['user']

Solution 3:

By doing tweet['user'] in the if statement you are assuming the key user exist, which raises the KeyError. You can test if the key is in the dict by doing if 'user' in tweet. Alternatively, you can handle the KeyError similar to how to handle ValueError

try:
    ....
    try:
        tweets_location.append( tweet['user']['location'] )
    except KeyError:
        tweets_location.append("")
except ValueError:
        pass

Post a Comment for "Parse Json To Csv Twitter Data Keyerror: 'user'"