python - KeyError in console, but key exists and function works fine -


i have looked @ other keyerror questions on here, couldn't find 1 addressed situation. pulling location information google , reading json info python list. here part of script goes iterates on results google.

results = []  # def get_data(): term, (latitude, longitude, search_radius) in search_parameters.items():     params = {         'query': term,         'location': (latitude, longitude),         'radius': search_radius     }     geocode_result = gmaps.places(**params)     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)             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) 

here example of json style python code pulling from.

[{   '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']   },  

i getting traceback error in terminal when run it, when print results returning no issues.

traceback (most recent call last):   file "testing.py", line 52, in <module>     results.append(i['price_level']) keyerror: 'price_level' 

actually "price_level" outside of list.

you'll have geocode_result['price_level']


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -