Reputation: 1848
I'm using google place API to return a list of locations. I would like to find the first name from the result set. I'm getting a list of values, but when I try to get just the first name, it gives me the first character instead.
name2 = simplejson.dumps([s['name'] for s in result['results']], indent=0)[0]
Obviously, there has to be a better way to get what I want, but I haven't found it. Seems like I'm missing something pretty basic. Following is the whole function:
import simplejson, urllib
PLACE_SEARCH = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
def placelatlng(name,city, state, sensor,**geo_args):
geo_args.update({
'name': name,
'city': city,
'state': state,
'sensor': sensor
})
concat=name+'+'+city+'+'+state
query = {"query": concat }
key="MyKey"
url = PLACE_SEARCH + '?' + urllib.unquote(urllib.urlencode(query))+ '&' + "sensor="+sensor +'&' + "key="+key
result = simplejson.load(urllib.urlopen(url))
name2 = simplejson.dumps([s['name'] for s in result['results']], indent=0)
Thanks.
Upvotes: 0
Views: 249
Reputation: 414565
dumps() returns a string. [0]
gets the first character in it.
To get the first result's name:
print result['results'][0]['name']
Upvotes: 2