I am trying to extract some user information from the tweets I have downloaded. Below is the code that I am using, however, it only returns "None" in the list for all the tweets.
map(lambda test: test['place']['country'] , data)
Also, tried the following:-
map(lambda test: test.get('place').get('country'), data)
I am relatively new to Python. It will be really helpful if someone can please help me understand if I am missing something here.
This will just be due to the fact that the vast majority of tweets do not seem to carry the 'place' data - Its nearly always set to None.
Your best bet for a location would be to use the location key inside the 'user' section of the data dump - but even that isnt always filled out (You would have to try/except to handle TypeErrors for None type objects. See below for what I use within my listener.
def on_data(self, data):
tweet = json.loads(data)
print "\n\n ***********************************"
try:
print tweet['created_at'][:19] + ' - ' + tweet["text"] + ' - ' + tweet['user']['location']
except TypeError:
print tweet['created_at'][:19] + ' - ' + tweet["text"] + ' - ' 'No Location Given'
return True
Hope this helps you on your way!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.