Extracting user information from tweets using Python
<p>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.</p>

<pre><code>map(lambda test: test['place']['country'] , data)
</code></pre>

<p>Also, tried the following:-    </p>

<pre><code>map(lambda test: test.get('place').get('country'), data)
</code></pre>

<p>I am relatively new to Python. It will be really helpful if someone can please help me understand if I am missing something here. </p>

<p>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.</p>

<p>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.</p>

<pre><code>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
</code></pre>

<p>Hope this helps you on your way!</p>

python python-2.7 python-3.x twitter