API-Call, Json und und Pygal - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: API-Call, Json und und Pygal (/thread-20865.html) |
API-Call, Json und und Pygal - starter_student - Sep-03-2019 Moin! hiermit entwickle ich eine jkleine WebApp mit Flask und da ich eine API verwenden möchte für eine Visualisierung von Wetter-Daten (Name, temp_min und temp_max) habe ich den folgenden Fehler erhalten und mit Json zu arbeiten ist ganz neu für mich! Danke für Rückmeldung. VG KeyError KeyError: 'name' @app.route("/weather") def weather(): url = "https://api.openweathermap.org/data/2.5/group?id=524901,703448,2643743&units=metric&appid=get a Key" r = requests.get(url) data = r.json() # Retrieve city names cities = [] for city in data["name"]: cities.append(city["name"]) # Retrieve temperatures min_temp, max_temp = [], [] for elt in data["main"]: min_temp.append(elt["temp_min"]) max_temp.append(elt["temp_max"]) #Graph #Define simple style, added with pygal.style import style = LS("#333666", base_style=DS) chart = pygal.Bar(style=style, x_label_rotation=45, show_legend=False) chart.title = "Minimum and Maximum Temperatures" chart.x_labels = map(str, cities) chart.add("Minimum Temperature", min_temp) chart.add("Maximum Temperature", max_temp) chart = chart.render_data_uri() return render_template('weather.html', title='Call the API', chart = chart) Json output
RE: API-Call, Json und und Pygal - buran - Sep-03-2019 line 10 should be for city in data["list"]: Note that you will need to change also line 17-19, in order to get min and max temp, but I will leave this to you for now RE: API-Call, Json und und Pygal - starter_student - Sep-04-2019 (Sep-03-2019, 03:59 PM)buran Wrote: line 10 should be Hey! Thank you! I didn't see that :) ... anyway I've changed it but till got the same KeyError: "list" # Retrieve city names cities = [] for city in data["list"]: cities.append(city["list"][1]["name"]) # Retrieve temperatures min_temp, max_temp = [], [] for elt in data["list"]: min_temp.append(elt["list"][0]["main"]["temp_min"]) max_temp.append(elt["list"][0]["main"]["temp_max"]) RE: API-Call, Json und und Pygal - buran - Sep-04-2019 (Sep-04-2019, 07:32 AM)starter_student Wrote: anyway I've changed it but till got the same KeyError: "list"it's on line 9 and 10 now. as I said you need to change these too. city_names = [] min_temps = [] max_temps = [] for city in data['list']: city_names.append(city['name']) min_temps.append(city['main']['temp_min']) max_temps.append(city['main']['temp_max']) print(city_names) print(min_temps) print(max_temps)
RE: API-Call, Json und und Pygal - starter_student - Sep-04-2019 (Sep-04-2019, 07:59 AM)buran Wrote:(Sep-04-2019, 07:32 AM)starter_student Wrote: anyway I've changed it but till got the same KeyError: "list"it's on line 9 and 10 now. as I said you need to change these too. Thks you Hero Please can you tell me what is wring with my code. Thks RE: API-Call, Json und und Pygal - buran - Sep-04-2019 (Sep-04-2019, 08:09 AM)starter_student Wrote: what is wring with my codethe way you try to access different parts in the JSON object, e.g. for city in data["list"]: cities.append(city["list"][1]["name"])you loop over each element in data['list']. That is each city data being dict . Then you try to access city['list'] - there is no such key within city dict. Then you even try (not sure what) to use index as in list i.e. [1] or key=1 as in dict with int keys... again - city is not a list, nor there is key=1 in city dictsame apply to part where you try to get temps RE: API-Call, Json und und Pygal - starter_student - Sep-04-2019 (Sep-04-2019, 08:16 AM)buran Wrote:(Sep-04-2019, 08:09 AM)starter_student Wrote: what is wring with my codethe way you try to access different parts in the JSON object, e.g. got it!. Thks |