Geopy Google v3 - 从 location.raw 中提取地址组件

Geopy Google v3 - Extracting address components from location.raw

我有这个 python 脚本,我从 SQL table 获取地址列表,然后使用 Geopy 将它们传递给谷歌 api 以对它们进行地理编码然后将数据写回不同的 SQL table.

我目前无法从 address_components 中提取地址部分。

我已经尝试了很多事情,比如使用其他地址解析器将 location.raw 转换为 Json(但我不在美国)而我的 python 不在强的。我也不能直接引用列表部分,因为不同的地址会有不同的长度,所以当我稍后将它应用于数据帧时,它会失败,因为列表的长度并不相同。例如 loc_raw0.append(location.raw['address_components'][0]['long_name'])

我目前正在尝试使用嵌套的 For 循环来获取街道号码,然后复制其他部分。

发生的事情是 k 将等于 'address_components' 但是 v 将等于 '{'long_name': '46', 'short_name': '46', 'types': ['street_number']}' 而不仅仅是 'street_number'.

        for k, v in location.raw.items():
            if k == 'address_components' and 'street_number' in v:
                loc_street_number.append(location.raw['long_name'])
                print(loc_street_number)

pic of list v contents

df = pd.DataFrame(SQL_Query, columns=['address'])


loc_Inputaddress = []
loc_Longitude = []
loc_Latitude = []
loc_Matchedaddress = []

loc_subpremise = []
loc_street_number = []
loc_road = []
loc_locality = []
loc_AdminArea1 = []
loc_AdminArea2 = []
loc_postcode = []
loc_type = []


for address in df.address:
    try:
        inputAddress = address
        location = g.geocode(inputAddress, timeout=15)

        loc_Inputaddress.append(inputAddress)
        loc_Longitude.append(location.longitude)
        loc_Latitude.append(location.latitude)
        loc_Matchedaddress.append(location.address)
        loc_type.append(location.raw['types'][0])

        #get address type
        print(loc_type.append(location.raw['types'][0]))

        #print(location.raw['address_components'])

  
        for k, v in location.raw.items():
            if k == 'address_components' and 'street_number' in v:
                loc_street_number.append(location.raw['long_name'])
                print(loc_street_number)

    except Exception as e:
        print('Error, skipping address...', e)

在您的代码中,v 是一个字典列表,据我了解,您想要字典的 long_name 具有 street_number 类型。这个例子应该可以帮助你:

v = [{
    "long_name": "40",
    "short_name": "40",
    "types": ["subpremise"]
},
{
    "long_name": "46",
    "short_name": "46",
    "types": ["street_number"]
},
{
    "long_name": "Aongatete",
    "short_name": "Aongatete",
    "types": ["locality", "political"]
}]

# Iterate over v because it is a list
for address_component in v:
    # Check if one of the address component types is "street_number"
    if "street_number" in address_component["types"]:
        print(address_component["long_name"])
        break

# Output:
# 46

Demo in myCompiler