我遇到了将json样式python代码转换为字典的问题。我正在使用来自Google的拉取位置数据并获取我需要的所有数据。但是,每当我从数据列表中创建字典时,都会丢失一些键:值对。
这是我回来的json样式python代码。
{
'html_attributions': [],
'next_page_token': 'CpQCBgEAAFzW9_qpkvmXV8sFiwXg6dEp2jsZXEqQOO4upW1U0FZPK0kiJvSmEbx4EuhAtDD4vtJcC4duqCj6oo0P68cRkGArenzZQWr61Wne0vA44R16SrfCDWLxQjqUu8zUcdLvdB7HfOHPMd7-KWK4cmX-6bLYIJldmnJBeqhnR5xmtB_TGlMyGlzh4vZ4kOB8dc1bnD6ijWxfFyz8wJ1ugA_5hZHLXtCY6NkE8TVYgTTV9710puKlKgiVSSkjwV3IsGgEhzixzGRrVHg2dB3Ehv2KHRuXnvIQJHr-CIkUoK79vI-45pyitdYlW1h9UQk_fzDsIeec3PNE5Qz6kN--NcDv-_jzFBr7QS2RGLkAcZX4aFdfEhCoP3MAVgjsl_tKdYaIX8y_GhSvCFZSxLfWVDAyF--7T6l32G0YFw',
'results': [{
'formatted_address': '2222 McKinney Ave, Dallas, TX 75201, United States',
'geometry': {
'location': {
'lat': 32.7935,
'lng': -96.8021119
},
'viewport': {
'northeast': {
'lat': 32.7948813802915,
'lng': -96.8009040197085
},
'southwest': {
'lat': 32.7921834197085,
'lng': -96.8036019802915
}
}
},
'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png',
'id': '26ba45975d09c8889ea321078b6bf10ceff07359',
'name': 'Smashburger',
'opening_hours': {
'open_now': True,
'weekday_text': []
},
'photos': [{
'height': 3024,
'html_attributions': ['<a href="https://maps.google.com/maps/contrib/107709113475800121508/photos">Terry Wint Jr</a>'],
'photo_reference': 'CmRaAAAA9BPb8sP5KUp9bLAgSd9dPQzH15TkG3sw5T6qOa-WSV1hi-3DLIhVzWI5woD2GtSl3aktKXTXktLUfr9vpum-XywbqFMKunee-YhWwPtwOGJaO5lULU-g1px7Z6lPUFvqEhDrofF3_89I455YRKo2hZuFGhRilVDqXdt5x2jPieN3W07UvJwsvA',
'width': 4032
}],
'place_id': 'ChIJ22Ci9y-ZToYRDw1hkXkjceY',
'price_level': 1,
'rating': 4.1,
'reference': 'CmRSAAAAzQIvy0nuhTQpRoAFTlilcVY_BfqGFj7gNlpNeIecwOWGV1x0qFm80zkPYFYKtJ15ilHYSoQbDH2wsQ_vahw5ilhriHA0EBWavcyev4mYVgNWssL0neY5d7YwOLAgtSEvEhBN6e_DakrfQPVtqkagEtQ4GhRCdi5cZ6CKfk6AqXkuGJ9ZVpuR-A',
'types': ['restaurant', 'food', 'point_of_interest', 'establishment']
}
正如我在另一个thread 'price_level'
中指出的那样,在'results'
列表之外。但是,每当我呼叫“geocode_result.keys()'
时,我只会获得'html_attributes', 'next_page_token', 'results', 'status'
。
这是我正在运行的代码。
results = []
# def get_data():
for term, (latitude, longitude, search_radius) in search_parameters.items():
params = {
'query': term,
'location': (latitude, longitude),
'radius': search_radius
}
geocode_result = gmaps.places(**params)
for i in geocode_result['results']:
results.append(i['name'])
results.append(i['geometry']['location']['lat'])
results.append(i['geometry']['location']['lng'])
results.append(i['formatted_address'])
results.append(i['opening_hours']['open_now'])
results.append(i['price_level'])
results.append(i['rating'])
results.append(i['types'])
if 'next_page_token' in geocode_result.keys():
while 'next_page_token' in geocode_result.keys():
time.sleep(2.5)
params.update({'page_token': geocode_result['next_page_token']})
geocode_result = gmaps.places(**params)
for i in geocode_result['results']:
results.append(i['name'])
results.append(i['geometry']['location']['lat'])
results.append(i['geometry']['location']['lng'])
results.append(i['formatted_address'])
results.append(i['opening_hours']['open_now'])
results.append(i['price_level'])
results.append(i['rating'])
results.append(i['types'])
else:
break
print(results)
我希望能够提取'lat', 'lng', 'name', 'open_now', 'price_level', 'rating', 'types'
。我可以调用除最后三个之外的所有内容,因为它们不会被存储为'geocode_result'
中的键值对。任何帮助或建议表示赞赏!
答案 0 :(得分:0)
&#34;是的,但是你在geocode_result中搜索[&#39;结果&#39;]所以你应该在geocode_result中查找我[&#39;结果&#39;]:print i.keys( )&#34; - @aws_apprentice
但事实证明,我收到错误的原因是并非所有结果都有&#39; price_level&#39;所以我将使用try语句来避免这个问题。