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:
  File "<stdin>", line 18, in <module>
  KeyError: 'user'
So I tried to add an if-else statement, but it is still giving me the error message. What is the next step?
for line in lines:
    try:
            tweet = json.loads(line)
            # Ignore retweets!
            if tweet.has_key("retweeted_status") or not tweet.has_key("text"):
                    continue
            # Fetch text from tweet
            text = tweet["text"].lower()
            # Ignore 'manual' retweets, i.e. messages starting with RT             
            if text.find("rt ") > -1:
                    continue
            tweets_text.append( text )
            # I added an if-else statement, but it's still having be the error message
            if tweet['user']:
                    tweets_location.append( tweet['user']['location'] )
            else:
                    tweets_location.append("")
    except ValueError:
            pass
 
     
     
     
     
    