如何将JSON字典列表解析为CSV

1 投票
2 回答
1591 浏览
提问于 2025-04-17 21:52

我从谷歌地点API获取了一个json对象。这个对象就像一个字典,有三个键:{'status', 'html_attributions', 'results'}。其中,results这个键包含了一个字典列表,里面有我需要的信息,也就是在'geography'字典里的纬度和经度。我需要把这些纬度和经度解析成一个csv文件,以便后续处理。到目前为止,我写的代码是:

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)

这段代码可以写出纬度和经度(虽然不是特别干净,但能完成任务)。不过,我需要对列表中的所有元素都这样处理。有没有什么建议?

这就是json对象的实际样子:

*{'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'}*

2 个回答

0

我找到了一种解决这个问题的新方法。下面是代码:

##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)
1

这看起来是有效的:

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'])

生成的test3.csv文件内容:

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

撰写回答