Question

I have a json object retrieved from the google places api. The object is a dictionary with three keys {'status', 'html_attributions', 'results'}. The key results contain a list of dictionaries with the information that I need which is the latitude, longitude inside the 'geography' dict. I need to parse the lat longitude to a csv file for later processing. So far this is the code that I have:

response = urllib2.urlopen(url)
result = response.read()
d = simplejson.loads(result)
g=d['results']    
y=g[0]
y
z=dict.items(y['geometry'])
with open('test3.csv','w') as f:
    w = csv.writer(f)
    w.writerows(z)

This writes the latitude and longitude (not perfectly clean but it does the job). However I need to do this for all the elements in the list. Any suggestions on how to do this?

This is how the json object actually looks like:

*{'html_attributions': [u'Listings by <a href="http://www.gelbeseiten.de/">GelbeSeiten\xaeVerlagen</a>'],
 'results': [{'geometry': {'location': {'lat': 52.164737, 'lng': 9.964918}},
              'icon': 'http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png',
              'id': '6632006e0c9e43ca1436804cb00b7babf1ea3c2a',
              'name': "McDonald's",
              'opening_hours': {'open_now': True},
              'photos': [{'height': 608,
                          'html_attributions': ['<a href="https://plus.google.com/106802039558898860276">Tarek Tounsi</a>'],
                          'photo_reference': 'CnRpAAAAz35IbT8YWMJYJp7pBJs-IeDu7fI0_c9bUsYZui2rPn3rSjLFAO8JqbI28pd0sr5Q25KUideKfq1oAKT_T9LUlyTMpciCZCynzXEE6fNfQAvmLwc78gbG515PLor_8B82NUHIl49HsxkMmPhmnk3m8BIQsHFRud-4_w9fhnTdW6E3zRoU2oKQj3kWfPYDdZ45H9Q1mAwAuQA',
                          'width': 1024}],
              'price_level': 1,
              'rating': 3.8,
              'reference': 'CnRrAAAA9DxXNvv_eFpLX9MjhiTgvR6_0wrl4KROEu1fmoVexrFXaNH88r6IHPMUPTONbuuKlfZBXXJ4byaDKty5niJmW6StJLQkHrCX1tqXE9lubrJY4yw32vq5n0Z37X00ulGsFB7xJe2ADD_jtNDdim4v9hIQHRxmz9XRuZw4U4QqRtljrhoUoULu8xeuYgi7qMUNArThb0bCjhk',
              'types': ['restaurant', 'food', 'establishment'],
              'vicinity': u'Bavenstedter Stra\xdfe 48, Hildesheim'},
             {'geometry': {'location': {'lat': 52.380744, 'lng': 9.861758}},
              'icon': 'http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png',
              'id': '60317f570db420888a7fba543683db2450750c68',
              'name': "McDonald's Restaurant",
              'opening_hours': {'open_now': False},
              'price_level': 1,
              'reference': 'CoQBdwAAALC9DEmsiTdQ9rSuogeUprKt-UCTNo5Jmwi7l1iUevq_TjNyi19DWraSBuJxZ67lV9GGICahVl_gI8rYk20AmbX8-jzmDay0aQZGCJZgKCU2JFjgFW5enaqSA6twat1kXDoSetimJbxioI3JlFHr3Lvdb2w6mSOpst4GKzBwRMSEEhCi_nAkNmCo0AikA-7oW-8YGhQLSxUZek9wlngI8YUYpwSMk4AuMw',
              'types': ['restaurant', 'food', 'establishment'],
              'vicinity': u'Kreisstra\xdfe 2, Hanover'},
             {'geometry': {'location': {'lat': 52.412797, 'lng': 9.734524}},
              'icon': 'http://maps.gstatic.com/mapfiles/place_api/icons/restaurant71.png'}*
Was it helpful?

Solution

This seems to work:

fields = 'lat', 'lng'
with open('test3.csv', 'wb') as csvfile:
    w = csv.writer(csvfile)
    w.writerow(fields)  # optional -- header row
    w.writerows(operator.itemgetter(*fields)(result['geometry']['location'])
                    for result in d['results'])

Contents oftest3.csvfile produced:

lat,lng
52.164737,9.964918
52.380744,9.861758
52.412797,9.734524

OTHER TIPS

So I got another way to work around this. This is the code:

##remember that d is the dictionary that has the key 'results', which is also a list of dictionaries
g=d['results']
z=[d['geometry']['location'] for d in g]
keys=['lat','lng']

with open('C:/Users/J/Desktop/test4.csv', 'wb') as csvfile:
     dict_writer = csv.DictWriter(csvfile, keys)         
     dict_writer.writer.writerow(keys)         
     dict_writer.writerows(z)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top