I'm sorry if I missed anything. Let me know and I'll try to get you the information. I'm using Python 3.12.0 and am trying to code a relatively simple weather program that pulls information using json. When I run the program, I get the following errors:
For gust:
Traceback (most recent call last):
File "C:\Users\Jdzenorb\Desktop\weather.py", line 72, in <module>
my_city = City("Indianapolis",39.777778,-86.177222,units="imperial",)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Jdzenorb\Desktop\weather.py", line 22, in __init__
self.get_data()
File "C:\Users\Jdzenorb\Desktop\weather.py", line 39, in get_data
self.gust = self.response_json["wind"]["gust"]
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'gust'
For description:
Traceback (most recent call last):
File "C:\Users\Jdzenorb\Desktop\weather.py", line 72, in <module>
my_city = City("Indianapolis",39.777778,-86.177222,units="imperial",)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Jdzenorb\Desktop\weather.py", line 22, in __init__
self.get_data()
File "C:\Users\Jdzenorb\Desktop\weather.py", line 44, in get_data
self.description = self.response_json["weather"]["description"]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
TypeError: list indices must be integers or slices, not str
Here is the code that I have written. I'm having issues with pulling the "gust" and "description" and with converting the time from the json string to a readable time.
import requests
class City:
def __init__(self,name,lat,lon,units="metric",pressure="standard",humidity="standard",
distance="metric",speed="metric",deg="standard",gust="metric",all="standard",sunrise="standard",
sunset="standard",timezone="standard",description="standard"):
self.name = name
self.lat = lat
self.lon = lon
self.units = units
self.pressure = pressure
self.humidity = humidity
self.distance = distance
self.speed = speed
self.deg = deg
self.gust = gust
self.all = all
self.sunrise = sunrise
self.sunset = sunset
self.timezone = timezone
self.description = description
self.get_data()
def get_data(self):
try:
response = requests.get(f"https://api.openweathermap.org/data/2.5/weather?units={self.units}&lat={self.lat}&lon={self.lon}&appid=634b43c85eda738d2acfabac85f322d4")
except ConnectionError:
print("No internet connection. Please connect to internet.")
self.response_json = response.json()
self.temp = self.response_json["main"]["temp"]
self.feels_like = self.response_json["main"]["feels_like"]
self.temp_min = self.response_json["main"]["temp_min"]
self.temp_max = self.response_json["main"]["temp_max"]
self.pressure = self.response_json["main"]["pressure"]
self.humidity = self.response_json["main"]["humidity"]
self.distance = self.response_json["visibility"]
self.speed = self.response_json["wind"]["speed"]
self.deg = self.response_json["wind"]["deg"]
self.gust = self.response_json["wind"]["gust"]
self.all = self.response_json["clouds"]["all"]
self.sunrise = self.response_json["sys"]["sunrise"]
self.sunset = self.response_json["sys"]["sunset"]
self.timezone = self.response_json["timezone"]
self.description = self.response_json["weather"]["description"]
def weather(self):
pass
def main(self):
units_symbol = "C"
if self.units == "imperial":
units_symbol = "F"
print(f"In {self.name} it is currently {self.temp}°{units_symbol}")
print(f"It feels like: {self.feels_like}°{units_symbol}")
print(f"Today's High: {self.temp_max}°{units_symbol}")
print(f"Today's Low: {self.temp_min}°{units_symbol}")
print(f"The Pressure is: {self.pressure}hPa")
print(f"The Humidity is: {self.humidity}%")
def visibility(self):
print(f"The visibility is: {self.distance}m")
def wind(self):
units_symbol = "m/s"
if self.units == "imperial":
units_symbol = "mph"
print(f"The wind speed is: {self.speed}{units_symbol} from {self.deg}°")
print(f"Wind Gusts up to: {self.gust}{units_symbol}")
def clouds(self):
print(f"Cloud cover: {self.all}%")
def sys(self):
print(f"Sunrise will occur at {self.sunrise} {self.timezone}")
print(f"Sunset will occur at {self.sunset} {self.timezone}")
my_city = City("Indianapolis",39.777778,-86.177222,units="imperial",)
my_city.weather()
my_city.main()
my_city.visibility()
my_city.wind()
my_city.clouds()
my_city.sys()
Here is the json data:
{
"coord": {
"lon": -86.1772,
"lat": 39.7778
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 285.33,
"feels_like": 283.73,
"temp_min": 282.36,
"temp_max": 286.85,
"pressure": 1027,
"humidity": 43
},
"visibility": 10000,
"wind": {
"speed": 2.57,
"deg": 240
},
"clouds": {
"all": 0
},
"dt": 1700063188,
"sys": {
"type": 2,
"id": 2089097,
"country": "US",
"sunrise": 1700051352,
"sunset": 1700087374
},
"timezone": -18000,
"id": 4259418,
"name": "Indianapolis",
"cod": 200
}
What I have tried:
For the gust, when I comment it out line 39 (and subsequently everything else to prevent errors, the code will run.
self.gust = self.response_json["wind"]["gust"]
For the description error, I can do the same thing as I do for gust (line 44) and get the same results.
self.description = self.response_json["weather"]["description"]
For the time, I've tried importing and using datetime and trying to format it as "%Y-%m-%dT%H:%M:%S.%f%z", but nothing will change it.