【问题标题】:Parse Google Geocode JSON in Python在 Python 中解析 Google 地理编码 JSON
【发布时间】:2016-02-03 15:00:32
【问题描述】:

我是 python 新手,正在尝试使用 Python 2.7 从 Google 地理编码响应中解析 JSON。完整的 JSON 文件可以在这里找到:https://dl.dropboxusercontent.com/u/60455118/data.json

示例 JSON:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.714232,
               "lng" : -73.9612889
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7155809802915,
                  "lng" : -73.9599399197085
               },
               "southwest" : {
                  "lat" : 40.7128830197085,
                  "lng" : -73.96263788029151
               }
            }
         },

我希望输出格式如下:

227, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, Latitude, Longitude

我的脚本如下:

import json
from pprint import pprint

with open('c:\scripts\data.json') as f:
   data = json.load(f)


coordinates = data["results"][0]["geometry"]["location"]


for i in data["results"][0]["address_components"]:
   print i["long_name"]

它以这种格式输出:

277
Bedford Avenu
Williamsburg
Brooklyn
Kings County
New York
United States
11211

如何转置它并添加 Lat Lon 值?

【问题讨论】:

    标签: python json parsing google-geocoder


    【解决方案1】:

    将值放入列表中,然后使用 ","join

    elements = []
    
    coordinates = data["results"][0]["geometry"]["location"]
    
    for i in data["results"][0]["address_components"]:
       elements.append( i["long_name"] )
    
    elements.append( str(coordinates['lat']) ) # convert to string to use `join`
    elements.append( str(coordinates['lng']) ) # convert to string to use `join`
    
    print ", ".join(elements)
    

    -

    277, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, 40.714232, -73.9612889
    

    【讨论】:

      【解决方案2】:

      我不明白你的问题是什么:

      for result in data["results"]:
          address = address = {place['types'][0]: place ['long_name'] for place in data["address_components"]}
          address['lat'] = float(result["geometry"]["location"]['lat'])
          address['lng'] = float(result["geometry"]["location"]['lng'])
          print address
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-06
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多