0

I am trying to get access to weather station data following the method mentioned on the website: https://towardsdatascience.com/getting-weather-data-in-3-easy-steps-8dc10cc5c859 I am using python for this. I am still learning python and I have no idea why it's showing error. The lines of code I used is:

r = requests.get('https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&datatypeid=\
                       TAVG&limit=1000&stationid=GHCND:USC00297461&startdate=2020-01-01&enddate=2020-10-22',
                       headers={'token':Token})
d = r.json()
avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

The error is:

KeyError                                  Traceback (most recent call last)
<ipython-input-85-c1a56919f6ab> in <module>()
      4 d = r.json()
      5 #get the item (average temperature) from the response
----> 6 avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

KeyError: 'results'

Summer
  • 11
  • 2

1 Answers1

0

You are getting an error because you should have this

avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

instead of this :

avg_temps = [item for item in d['results'] if d['datatype']=='TAVG']

difference is item['datatype'] instead of d['datatype']

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • This solution didn't work for me. I'm using the one you suggest (I tried both just to be sure). It works for newer data (2020 for example) but not for older data (1978). For the older data, I'm still getting the keyerror mentioned in the OP. – Litmon Nov 24 '21 at 15:00