-2

I've been trying to reverse geocode a csv of lat and long data to find the postal codes that correspond to the coordinates. I have an API from google and have attempted using pygeocoder, however it fails to recognise an API (see code below).

import pandas as pd
from pygeocoder import Geocoder 
import numpy as np



lndn = pd.read_csv(r'my file pathway', sep=',')

results = Geocoder.reverse_geocode(lndn['lat'][0], lndn['lng'][0])

Everytime I received and error:

GeocoderError: Error REQUEST_DENIED
Query: https://maps.google.com/maps/api/geocode/json?region=&latlng=51.495290%2C-0.166223&sensor=false&bounds=&language=

I then used googlemaps package:

import googlemaps 
import pandas as pd

gmaps = googlemaps.Client(key='my API')

lndn = pd.read_csv(r'my file pathway', sep=',')

results = gmaps.reverse_geocode((lndn['lat'][0], lndn['lng'][0]))

results 

This worked, however just printed all the info i.e.

[{u'address_components': [{u'long_name': u'35',
    u'short_name': u'35',
    u'types': [u'street_number']},
   {u'long_name': u'Walton Street',
    u'short_name': u'Walton St',
    u'types': [u'route']},
   {u'long_name': u'Chelsea',
    u'short_name': u'Chelsea',
    u'types': [u'neighborhood', u'political']},
   {u'long_name': u'London',
    u'short_name': u'London',
    u'types': [u'postal_town']},
   {u'long_name': u'Greater London',
    u'short_name': u'Greater London',
    u'types': [u'administrative_area_level_2', u'political']},
   {u'long_name': u'England', etc............

I can see that the postal code is in there. Is there a way to extract and print JUST the postal code from all this?

CWC_23
  • 1
  • 1

1 Answers1

0

if you don't mind using libraries...

import json, ast
x = ast.literal_eval(json.dumps(vals))

for i in x:
    print(i)

This will print out a dictionary without the unicode 'u' of all the values.

will
  • 58
  • 1
  • 6