Wednesday, 15 June 2011

How Can I Parse Google Maps API Data In Python? -



How Can I Parse Google Maps API Data In Python? -

i trying parse json info receiving

{'results': [{'address_components': [{'long_name': 'new york', 'short_name': 'new york', 'types': ['locality', 'political']}, {'long_name': 'new york', 'short_name': 'ny', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'united states', 'short_name': 'us', 'types': ['country', 'political']}], 'formatted_address': 'new york, ny, usa', 'geometry': {'bounds': {'northeast': {'lat': 40.9152414, 'lng': -73.70027209999999}, 'southwest': {'lat': 40.495908, 'lng': -74.2590879}}, 'location': {'lat': 40.7143528, 'lng': -74.00597309999999}, 'location_type': 'approximate', 'viewport': {'northeast': {'lat': 40.9152414, 'lng': -73.70027209999999}, 'southwest': {'lat': 40.4959143, 'lng': -74.2557349}}}, 'types': ['locality', 'political']}, {'address_components': [{'long_name': 'manhattan', 'short_name': 'manhattan', 'types': ['sublocality', 'political']}, {'long_name': 'new york', 'short_name': 'new york', 'types': ['locality', 'political']}, {'long_name': 'new york', 'short_name': 'new york', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'new york', 'short_name': 'ny', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'united states', 'short_name': 'us', 'types': ['country', 'political']}], 'formatted_address': 'manhattan, new york, ny, usa', 'geometry': {'bounds': {'northeast': {'lat': 40.882214, 'lng': -73.907}, 'southwest': {'lat': 40.6795479, 'lng': -74.047285}}, 'location': {'lat': 40.7902778, 'lng': -73.9597222}, 'location_type': 'approximate', 'viewport': {'northeast': {'lat': 40.820045, 'lng': -73.90331300000001}, 'southwest': {'lat': 40.698078, 'lng': -74.03514899999999}}}, 'partial_match': true, 'types': ['sublocality', 'political']}], 'status': 'ok'} [ { "lat": 40.7143528, "lng": -74.00597309999999 }, { "lat": 40.7902778, "lng": -73.9597222 } ]

how able parse "results" example? print out info within results. code is...

import urllib, json import pprint url2 = "http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=false" googleresponse = urllib.urlopen(url2) jsonresponse = json.loads(googleresponse.read()) pprint.pprint(jsonresponse) test = json.dumps([s['geometry']['location'] s in jsonresponse['results']], indent=3) print test['results']

the error

traceback (most recent phone call last):

file "c:/python27/jstox.py", line 10, in

print test['results']

typeerror: string indices must integers, not str

you getting results want.

import urllib, json import pprint url2 = "http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=false" googleresponse = urllib.urlopen(url2) jsonresponse = json.loads(googleresponse.read()) pprint.pprint(jsonresponse) pprint.pprint(jsonresponse['results'])

json info can treated python dictionary in case, if you're familiar those.

python google-maps

No comments:

Post a Comment