2016-05-12 7 views
3

Ich versuche, die JSON-Antwort von der GoogleMaps-API zu analysieren, um Werte aus einem verschachtelten Dict in einen Pandas DataFrame zu lesen. Die Struktur der JSON-Antwort ist unten gezeigt.GoogleMaps API Json Parsing in Python mit Ijson

Ich versuche, einen DataFrame namens 'address_components' mit ('types', 'short_name', 'long_name') als Spaltenüberschriften und die entsprechenden Werte von jeder Adresse Abfrage als Zeilen zu erstellen.

Ich habe versucht, ijson zu verwenden:

from ijson import items 
import ijson 
f = urlopen('https://maps.googleapis.com/maps/api/geocode/json?address=2022+Boren+Ave%2C+Seattle+98101') 

objects = list(ijson.items(f, 'results.address_components')) 

objects 

Out: []

Mein Problem erfolgreich die verschachtelten dicts überquert.

Ich habe auch die GoogleMaps GeoLocator API Documentation und die GoogleMaps JSON parsing docs überprüft. This solution seems to solve the problem, but on IOS

mir nicht zu helfen in das Nest zu erreichen ...

"results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "1600", 
       "short_name" : "1600", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Amphitheatre Pkwy", 
       "short_name" : "Amphitheatre Pkwy", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Mountain View", 
       "short_name" : "Mountain View", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Santa Clara County", 
       "short_name" : "Santa Clara County", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "California", 
       "short_name" : "CA", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "94043", 
       "short_name" : "94043", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", 
     "geometry" : { 
      "location" : { 
       "lat" : 37.4224764, 
       "lng" : -122.0842499 
      }, 
      "location_type" : "ROOFTOP", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 37.4238253802915, 
        "lng" : -122.0829009197085 
       }, 
       "southwest" : { 
        "lat" : 37.4211274197085, 
        "lng" : -122.0855988802915 
       } 
      } 
     }, 
     "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", 
     "types" : [ "street_address" ] 
     } 
    ], 
    "status" : "OK" 
} 

Antwort

0

Sie json_normalize() dafür verwenden können ::

import requests 
import json 
import pandas as pd 

url = 'https://maps.googleapis.com/maps/api/geocode/json?address=2022+Boren+Ave%2C+Seattle+98101' 

r = requests.get(url) 

if r.status_code != requests.codes.ok: 
    r.raise_for_status() 

results = json.loads(r.text) 

df = pd.io.json.json_normalize(results['results'], 'address_components') 

print(df) 

Test:

In [17]: pd.io.json.json_normalize(results['results'], 'address_components') 
Out[17]: 
     long_name short_name          types 
0   2022   2022       [street_number] 
1 Boren Avenue Boren Ave         [route] 
2  Belltown  Belltown     [neighborhood, political] 
3  Seattle  Seattle      [locality, political] 
4 King County King County [administrative_area_level_2, political] 
5  Washington   WA [administrative_area_level_1, political] 
6 United States   US      [country, political] 
7   98121  98121        [postal_code] 
+0

Fantastic! Ich danke dir sehr. Genau das habe ich gesucht. Anscheinend darf ich das noch nicht offiziell sagen, als ich bei der Überprüfung der Box sagte, ich sei kein glaubwürdiger Befragter auf meine Frage ... Jedenfalls vielen Dank. –