# importing required libraries
import requests
import simplekml
# defining important variables
kml = simplekml.Kml()
zip_codes = ['20130', '22602', '99123', '12abc', '39654', '20186']
# creating the core function
def get_coords(zip_codes):
url = f"https://api.zippopotam.us/us/{zip_codes}"
response = requests.get(url)
# collecting required information
if response.status_code == 200:
data = response.json()
Places = data['places'][0]
Longitude = Places['longitude']
Latitude = Places['latitude']
City = Places['place name']
State = Places['state']
return Longitude, Latitude, City, State
else:
return f"Error: {response.status_code},could not retrieve data." # filtering out bad inputs
# a for loop to check each specific value in the zip_codes tuple
for codes in zip_codes:
if len(get_coords(codes)) == 4: # if/else solved error that occured when a zip code was not valid
print(get_coords(codes)) # a check to make sure the program is running smoothly
Longitude, Latitude, City, State = get_coords(codes) # unpacks returned tuple from the previous function
Location = [City, State] # repacks city and state variables into the "Location" tuple
kml.newpoint (name = codes, description = Location,
coords = [(Longitude, Latitude)]) # gives the KML file the required data
else:
print(get_coords(codes))
kml.save('GoogleEarth.kml')
< /code>
Прямо сейчас этот блок принимает почтовые индексы и вызывает Zippotam.us, чтобы получить данные местоположения, а затем преобразует эту информацию в файл KML. Мне просто нужно изменить его, получить почтовый индекс из данных о местоположении. Так что я еще ничего не пробовал из -за того, что не знал, что делать.
Подробнее здесь: https://stackoverflow.com/questions/796 ... -in-zip-co
Мне нужно отменить этот блок функциональности кодов. Вместо того, чтобы принимать почтовые индексы и предоставлять инфор ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение