这就是我在做的事情:
def getCounty(data):
try:
#print(data["geo"]["coordinates"]) # Getting Lat/Long coordinates
geo_data = data["geo"]["coordinates"]
geo_detail = geolocator.reverse(geo_data)
except TypeError,e:
geo_data = "null"
geo_detail = "N/A"
ctr = 0
i = 0
j = 0
if(geo_detail != "N/A"):
while(geo_detail[i]!= ',' and ctr == 4):
j=j+1
if(ctr == 3):
i=j+1
if(geo_detail[j] == ','):
ctr = ctr+1
county = geo_detail
else:
county = "No Location Provided"
return county[i:j]
由于某种原因,这个返回
()
当我做:
return county
时,它确实从这些坐标中返回了所有信息。
我似乎无法获得变量的子字符串。
我如何获得县属性,或者我可以得到哪些其他方法?
将输出字符串放在逗号上,并获得第四索引(县):
county
我只是想补充一点,这并不总是适用于所有地址。有些回报不包括街道号码(即使在查找中提供),然后将返回状态。 try:
return geo_detail.address.split(",")[3] # Milwaukee County
由于位置并非全部以相同的格式使用:
[x.strip() for x in location.address.split(',') if 'County' in x ][0]
这对我有用:
location = geolocator.reverse("47.173056,-114.553611")
line = location.address
line = line.replace(',','')
words = line.lower().split()
print(words)
ind = (words.index('county')-1)
print(words[ind])