This is a Python3 / json / programming question Hello I have a json file with a
ID: 3756798 • Letter: T
Question
This is a Python3 / json / programming question
Hello I have a json file with a list of cities formated this way se exampe below. I need to know how to create a method with all the cities so when I execute that method it will hit each latitud and longitude so It automatically will do a request for each city to an API. I already have the API set up but just asking for one city and try some solutions with a for loop but unfortunatelly isn't working. Thanks in advance for your help!
{
"city": "Louisville/Jefferson County",
"growth_from_2000_to_2013": "10.0%",
"latitude": 38.2526647,
"longitude": -85.7584557,
"population": "609893",
"rank": "28",
"state": "Kentucky"
},
{
"city": "Portland",
"growth_from_2000_to_2013": "15.0%",
"latitude": 45.5230622,
"longitude": -122.6764816,
"population": "609456",
"rank": "29",
"state": "Oregon"
},
Explanation / Answer
""" sorry if i did't your question well but as far as i understood you have an API which recieves the latitide and longtitude of and you need to call for each cities so first of all you need to put the cities into the python list [] and then can using the for loop callget the latitude and longtitude of each city and call the API in below request call chnage the api both details as header """ # code starts from here cities = [ { "city": "Louisville/Jefferson County", "growth_from_2000_to_2013": "10.0%", "latitude": 38.2526647, "longitude": -85.7584557, "population": "609893", "rank": "28", "state": "Kentucky" }, { "city": "Portland", "growth_from_2000_to_2013": "15.0%", "latitude": 45.5230622, "longitude": -122.6764816, "population": "609456", "rank": "29", "state": "Oregon" }, ] import requests def demo1(): # looping over all cities for city in cities: # for each city attribute can access using the city header = {"latitude": city['latitude'], "longitude": city["longitude"]} # change the api below resp = requests.get("TODO chnage the API", headers=header) # do with the resp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.