user2540748
user2540748

Reputation:

Coordinates from twitter location data

I'm using Tweepy to pull the coordinates of a tweet. It looks like this after using json to load it.

tweet = json.loads(data)
print(tweet['coordinates'])
{'coordinates': [-86.771035, 39.514024], 'type': 'Point'}

I want to store the longitude and latitude into a database, how would I directly access the coordinates? I tried using tweet['coordinates'][0] and various variations but it doesn't seem to be working the way I want.

Upvotes: 0

Views: 2721

Answers (2)

Naomi Fridman
Naomi Fridman

Reputation: 2144

You can get coordinates data in the following way:

for tweet in tweepy.Cursor(api.search,q='love',count = num_tweets).items():
    if (tweet.coordinates is not None): 
        lon = tweet.coordinates['coordinates'][0]
        lat = tweet.coordinates['coordinates'][1]

It is strange, but I had to try many forms and ways to make it work.

Upvotes: 2

Yuri Malheiros
Yuri Malheiros

Reputation: 1410

Try:

latitude, longitude = tweet["coordinates"]["coordinates"]

Because tweet["coordinates"] returns other dictionary that has a key also called "coordinates" with a list as its value.

Upvotes: -1

Related Questions