【问题标题】:How to extract a specific data from the json using python [closed]如何使用python从json中提取特定数据[关闭]
【发布时间】:2016-10-18 07:35:27
【问题描述】:

我想提取下面给定 json 中“address_components”中存在的“long_name”数据。挑战是我只想要“类型”为“administrative_area_level_2”的“long_name”数据。请让我知道如何最好在 python 中只提取特定数据。谢谢

编辑:请注意, js["results"][0]["address_components"][2]["long_name"] 不是我想要提取其值的方式,因为 ["address_components"][2] 不断变化作为输入给出的不同位置。我的主要标准是提取“long_name”的数据,其“types”总是“administrative_area_level_2”

{
    "status": "OK", 
    "results": [
        {
            "geometry": {
                "location_type": "APPROXIMATE", 
                "bounds": {
                    "northeast": {
                        "lat": 12.9177876, 
                        "lng": 80.24104
                    }, 
                    "southwest": {
                        "lat": 12.875989, 
                        "lng": 80.20860669999999
                    }
                }, 
                "viewport": {
                    "northeast": {
                        "lat": 12.9177876, 
                        "lng": 80.24104
                    }, 
                    "southwest": {
                        "lat": 12.875989, 
                        "lng": 80.20860669999999
                    }
                }, 
                "location": {
                    "lat": 12.9009877, 
                    "lng": 80.2279301
                 }
            }, 
            "formatted_address": "Sholinganallur, Chennai, Tamil Nadu, India", 
            "place_id": "ChIJGzh_3nlbUjoRGz_-itQtu_8", 
            "address_components": [
                {
                    "long_name": "Sholinganallur", 
                    "types": [
                        "sublocality_level_1", 
                        "sublocality", 
                        "political"
                    ], 
                    "short_name": "Sholinganallur"
                }, 
                {
                    "long_name": "Chennai", 
                    "types": [
                        "locality", 
                        "political"
                    ], 
                    "short_name": "Chennai"
                }, 
                {
                    "long_name": "Kanchipuram", 
                    "types": [
                        "administrative_area_level_2", 
                        "political"
                    ], 
                    "short_name": "Kanchipuram"
                }, 
                {
                    "long_name": "Tamil Nadu", 
                    "types": [
                        "administrative_area_level_1", 
                        "political"
                    ], 
                    "short_name": "TN"
                }, 
                {
                    "long_name": "India", 
                    "types": [
                        "country", 
                        "political"
                    ], 
                    "short_name": "IN"
                }
            ], 
            "partial_match": true, 
            "types": [
                "sublocality_level_1", 
                "sublocality", 
                "political"
            ]
        }
    ]
}

下面是我正在尝试的代码,但我只得到输出直到位置。让我知道我需要对代码进行哪些更改。

import urllib
import json

apiurl= 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break

    url = apiurl + urllib.urlencode({'sensor':'false', 'address':     address})
    print 'Retrieving', url
    uh = urllib.urlopen(url).read()

    print 'Retrieved',len(uh),'characters'
    js = json.loads(str(uh))
    print json.dumps(js, indent=4)   
    jayz = js["results"][0]
    lat = jayz["geometry"]["location"]["lat"]
    lng = jayz["geometry"]["location"]["lng"]
    print 'lat',lat,'lng',lng
    location = jayz['formatted_address']
    print location
    components = js["results"][0]["address_components"]
    names = [component['long_name'] for component in components if 'administrative_area_level_2' in component['types']]
    name = names[0]
    print "District:", name

【问题讨论】:

    标签: javascript python json api google-maps


    【解决方案1】:

    您可以获取组件并在它们上循环并找到您想要的组件

    js = eval(input_file.read())
    components =  js["results"][0]["address_components"]
    names = [component['long_name'] for component in components if 'administrative_area_level_2' in component['types']]
    

    在这里,名称将包括其类型中具有管理区域级别_2 的所有组件的 long_name

    【讨论】:

    • 嘿,非常感谢您的帮助。但你能根据我上面的代码回答吗?提前致谢
    • 它基于您的代码! js 是你的 json 测试评估成一个字典。我可以更新
    • 嘿 Niki 成功了,非常感谢您的帮助
    【解决方案2】:

    如果您喜欢,这是一种衬垫解决方案

    它在python中

    my_long_names = [[component.get("long_name") for component  in result.get("address_components") if "administrative_area_level_2" in component.get("types")] for result in data.get("results") ]
    print my_long_names
    

    【讨论】:

    • 嘿,非常感谢您的帮助。但你能根据我上面的代码回答吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2020-07-13
    • 2020-02-02
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多