Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用geopy提供的原始地址数据解析字典_Python_Google Maps_Geocoding_Geopy - Fatal编程技术网

Python 使用geopy提供的原始地址数据解析字典

Python 使用geopy提供的原始地址数据解析字典,python,google-maps,geocoding,geopy,Python,Google Maps,Geocoding,Geopy,我正试图得到一个地址列表的县名。为此,我使用geopy和raw方法将地址传递给Google地理编码API。问题是通过字典进行解析以获取该数据块 我需要的部分,是在行政区级别2,但这是一个列表现在。我无法按列表中项目的顺序进行分析,因为所有地址的格式都不同。通过这个解析的最佳方法是什么 import json places = ['901 Main St Dallas, TX'] geolocator = GoogleV3(api_key='') location = geolocator.ge

我正试图得到一个地址列表的县名。为此,我使用geopy和raw方法将地址传递给Google地理编码API。问题是通过字典进行解析以获取该数据块

我需要的部分,是在行政区级别2,但这是一个列表现在。我无法按列表中项目的顺序进行分析,因为所有地址的格式都不同。通过这个解析的最佳方法是什么

import json
places = ['901 Main St Dallas, TX']

geolocator = GoogleV3(api_key='')
location = geolocator.geocode(places, language='en')

print location.raw['address_components']```

This is the result that I am getting:

[{u'long_name': u'Bank of America Plaza', u'types': [u'premise'], u'short_name': u'Bank of America Plaza'}, {u'long_name': u'901', u'types': [u'street_number'], u'short_name': u'901'}, {u'long_name': u'Main Street', u'types': [u'route'], u'short_name': u'Main St'}, {u'long_name': u'Downtown', u'types': [u'neighborhood', u'political'], u'short_name': u'Downtown'}, {u'long_name': u'Dallas', u'types': [u'locality', u'political'], u'short_name': u'Dallas'}, {u'long_name': u'Dallas County', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Dallas County'}, {u'long_name': u'Texas', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'TX'}, {u'long_name': u'United States', u'types': [u'country', u'political'], u'short_name': u'US'}, {u'long_name': u'75202', u'types': [u'postal_code'], u'short_name': u'75202'}]

您可以像这样解析列表:

l = []
for i in c:
  for k, v in i.items():
    if k == 'types' and 'administrative_area_level_2' in v:
      l.append(i['long_name'])
print(l)

结果的可能重复出现的是“政治”,这是“行政区域级别2”之后的项目,那么您想要什么输出?“达拉斯县”的效果非常好!先生,你绝对是个芭蕾舞者!