我无法弄清楚如何解析由列表和字典组成的 API 响应以获取详细信息 - Python

I am unable to figure out how to parse through an API response consisting of lists and dictionaries to get details - Python

提问人:Abdullah G 提问时间:9/25/2023 最后编辑:Abdullah G 更新时间:9/25/2023 访问量:30

问:

如何从列表中的每个结果中选择所有位置、评级和名称,并将其组合成变量(名称、评级、位置)。

榜单(places_info)的前 2 名结果:

[{'results': [{'location': {'address': '150 Greenwood Ave',
     'country': 'CA',
     'cross_street': 'Dundas St E',
     'formatted_address': '150 Greenwood Ave (Dundas St E), Toronto ON M4L 2R1',
     'locality': 'Toronto',
     'postcode': 'M4L 2R1',
     'region': 'ON'},
    'name': 'Leslieville Farmers Market',
    'rating': 9.1},
   {'location': {'address': '1001 Eastern Ave',
     'country': 'CA',
     'cross_street': 'Woodfield Rd',
     'formatted_address': '1001 Eastern Ave (Woodfield Rd), Toronto ON M4L 1A8',
     'locality': 'Toronto',
     'postcode': 'M4L 1A8',
     'region': 'ON'},
    'name': 'Rorschach Brewing',
    'rating': 8.7},
   {'location': {'address': '1483 Queen St E',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1483 Queen St E, Toronto ON M4L 1E2',
     'locality': 'Toronto',
     'postcode': 'M4L 1E2',
     'region': 'ON'},
    'name': 'Chick-N-Joy',
    'rating': 7.6},
   {'location': {'address': '1592 Queen St E',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1592 Queen St E, Toronto ON M4L 1G1',
     'locality': 'Toronto',
     'postcode': 'M4L 1G1',
     'region': 'ON'},
    'name': 'Velotique',
    'rating': 7.4},
   {'location': {'address': '6 Coxwell Ave',
     'country': 'CA',
     'cross_street': 'Queen',
     'formatted_address': '6 Coxwell Ave (Queen), Toronto ON M4L 3A7',
     'locality': 'Toronto',
     'postcode': 'M4L 3A7',
     'region': 'ON'},
    'name': 'O Sushi',
    'rating': 7.5},
   {'location': {'address': 'Queen St',
     'country': 'CA',
     'cross_street': 'at Kingston Rd',
     'formatted_address': 'Queen St (at Kingston Rd), Toronto ON M4L 1G7',
     'locality': 'Toronto',
     'postcode': 'M4L 1G7',
     'region': 'ON'},
    'name': 'Woodbine Park',
    'rating': 8.5},
   {'location': {'address': '1374 Queen St E',
     'country': 'CA',
     'cross_street': 'at Greenwood Ave',
     'formatted_address': '1374 Queen St E (at Greenwood Ave), Toronto ON M4L 1C9',
     'locality': 'Toronto',
     'postcode': 'M4L 1C9',
     'region': 'ON'},
    'name': 'The Sidekick',
    'rating': 8.8},
   {'location': {'address': '1636 Queen St E',
     'country': 'CA',
     'cross_street': 'btwn Coxwell Ave. & Kingston Rd.',
     'formatted_address': '1636 Queen St E (btwn Coxwell Ave. & Kingston Rd.), Toronto ON M4L 1G3',
     'locality': 'Toronto',
     'postcode': 'M4L 1G3',
     'region': 'ON'},
    'name': "The Burger's Priest",
    'rating': 7.6},
   {'location': {'address': '1646 Queen St E',
     'country': 'CA',
     'cross_street': 'at Eastern Ave,',
     'formatted_address': '1646 Queen St E (at Eastern Ave,), Toronto ON M4L 1G3',
     'locality': 'Toronto',
     'postcode': 'M4L 1G3',
     'region': 'ON'},
    'name': 'Casa Di Giorgios',
    'rating': 7.4},
   {'location': {'address': '73 Coxwell Ave',
     'country': 'CA',
     'cross_street': 'Dundas St & Coxwell St',
     'formatted_address': '73 Coxwell Ave (Dundas St & Coxwell St), Toronto ON M4L 3B1',
     'locality': 'Toronto',
     'postcode': 'M4L 3B1',
     'region': 'ON'},
    'name': 'British Style Fish & Chips',
    'rating': 7.6}],
  'context': {'geo_bounds': {'circle': {'center': {'latitude': 43.665269,
      'longitude': -79.319796},
     'radius': 1000}}}},
 {'results': [{'location': {'address': '31 Powerhouse St',
     'country': 'CA',
     'cross_street': 'Lansdowne Ave',
     'formatted_address': '31 Powerhouse St (Lansdowne Ave), Toronto ON M6H 0C7',
     'locality': 'Toronto',
     'postcode': 'M6H 0C7',
     'region': 'ON'},
    'name': "Balzac's Coffee Roasters",
    'rating': 8.4},
   {'location': {'address': '330 Geary Ave',
     'country': 'CA',
     'cross_street': 'at Lightbourn Ave.',
     'formatted_address': '330 Geary Ave (at Lightbourn Ave.), Toronto ON M6H 2C7',
     'locality': 'Toronto',
     'postcode': 'M6H 2C7',
     'region': 'ON'},
    'name': 'Rehearsal Factory',
    'rating': 6.7},
   {'location': {'address': '1200 Lansdowne Ave',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1200 Lansdowne Ave, Toronto ON M6H 3Z8',
     'locality': 'Toronto',
     'postcode': 'M6H 3Z8',
     'region': 'ON'},
    'name': 'Earlscourt Park Off-Leash Area',
    'rating': 6.2},
   {'location': {'address': '229 Geary Ave',
     'country': 'CA',
     'cross_street': 'at Dufferin St',
     'formatted_address': '229 Geary Ave (at Dufferin St), Toronto ON M6H 2C1',
     'locality': 'Toronto',
     'postcode': 'M6H 2C1',
     'region': 'ON'},
    'name': 'The Greater Good Bar',
    'rating': 8.9},
   {'location': {'address': '1311 Saint Clair Ave W',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1311 Saint Clair Ave W, Toronto ON M6E 1C2',
     'locality': 'Toronto',
     'postcode': 'M6E 1C2',
     'region': 'ON'},
    'name': 'Tre Mari Bakery',
    'rating': 9.0},
   {'location': {'address': '1200 Lansdowne Ave',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1200 Lansdowne Ave, Toronto ON M6H 3Z8',
     'locality': 'Toronto',
     'postcode': 'M6H 3Z8',
     'region': 'ON'},
    'name': 'Earlscourt Park',
    'rating': 7.5},
   {'location': {'address': '1359 Davenport Rd',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '1359 Davenport Rd, Toronto ON M6H 2H5',
     'locality': 'Toronto',
     'postcode': 'M6H 2H5',
     'region': 'ON'},
    'name': 'The Sovereign',
    'rating': 7.3},
   {'location': {'address': '217 Geary Ave',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '217 Geary Ave, Toronto ON M6H 2C1',
     'locality': 'Toronto',
     'postcode': 'M6H 2C1',
     'region': 'ON'},
    'name': 'Parallel',
    'rating': 8.2},
   {'location': {'address': '1256 Saint Clair Ave W',
     'country': 'CA',
     'cross_street': 'at Boon Ave.',
     'formatted_address': '1256 Saint Clair Ave W (at Boon Ave.), Toronto ON M6E 1B9',
     'locality': 'Toronto',
     'postcode': 'M6E 1B9',
     'region': 'ON'},
    'name': 'Rio 40 Restaurant',
    'rating': 8.4},
   {'location': {'address': '229 Geary Ave',
     'country': 'CA',
     'cross_street': '',
     'formatted_address': '229 Geary Ave, Toronto ON M6H 2C1',
     'locality': 'Toronto',
     'postcode': 'M6H 2C1',
     'region': 'ON'},
    'name': 'North Of Brooklyn Pizzeria',
    'rating': 7.3}],
  'context': {'geo_bounds': {'circle': {'center': {'latitude': 43.67142,
      'longitude': -79.445947},
     'radius': 1000}}}},
....

我希望能够执行类似于以下代码的操作,但获取每个结果的所有值,而不仅仅是第一个列表中的值。列表太多,无法手动为每个列表编写索引。

results = [place['results'] for place in places_info]
names = [place[0]['name'] for place in results]
ratings = [place[0]['ratings'] for place in results]
locations = [place[0]['locations'] for place in results]

正确的方法是什么?我对编码相当陌生,所以任何帮助将不胜感激!

JSON的 列表 字典 数据科学

评论

0赞 Сергей Кох 9/25/2023
将 json 加载到数据帧中并执行必要的操作。

答:

0赞 Rufus 9/25/2023 #1

由于数据结构在两个级别具有数组,因此使用嵌套的 for 循环将结果平展为单个数组会更容易。

flat_results = []
for place in places:
    results = place['results']
    for result in results:
        flat_results.append(result)

#array notation alternative
alt_flat_results = [result for result in place['results'] for place in places]

这将为您提供一个包含嵌套在响应中的所有结果对象的数组。然后,您可以使用如下内容获取您喜欢的任何字段:

locations = [result['location'] for result in flat_results]