Error with precipi

  • 1
  • Question
  • Updated 7 months ago
My python code gives the error, KeyError: 'precipi', when running the following code:

dayconditions =(date+ "," + data['history']['observations'][yy]['hum']+ "%" + "," +data['history']['observations'][yy]['wdire']+ "," + data['history']['observations'][yy]['wspdi']+ " mph" + data['history']['observations'][yy]['precipi']+ " in" ) 

The code works to get the correct humidity and wind data, but gives an error when I add the last part. Can anyone help me with what I'm doing wrong as far as the precipitation rate goes? 

Thanks!
Photo of Rebecca Marie Bullard

Rebecca Marie Bullard

  • 1 Post
  • 0 Reply Likes

Posted 8 months ago

  • 1
Photo of Richard Elkins

Richard Elkins

  • 8 Posts
  • 0 Reply Likes
Maybe, no element "precipi" exists at the JSON level you are expecting.

I suggest that you post the exact HTTP request string.  I'll try it myself.  Also, you might post the HTTP reply string when it works as expected.

By the way, aeson-pretty is an excellent JSON "pretty printer".  I use it to fix up my JSON replies (often ugly) from NASA and Wunderground.  It's available on Unix and Linux distros.  Probably Mac.  I do not know about Windows.  Web site: https://github.com/informatikr/aeson-pretty
Photo of Richard Elkins

Richard Elkins

  • 8 Posts
  • 0 Reply Likes
Just a small nit but I think that you meant to include +"," after " mph" to separate wind data from precipitation rate with a comma.  Right?  

When there is no precipitation (like frequently here in Dallas), I see:
  • ["observations"]00["precipi"] = "-9999.00" 
  • ["dailysummary"]00["precipi"] = "0.00" 
(Edited)
Photo of Richard Elkins

Richard Elkins

  • 8 Posts
  • 0 Reply Likes
import urllib2, json, sys

url1 = "http://api.wunderground.com/api/"
wunderground_api_key = "YOUR KEY"
url2 = "/history_20170901/q/TX/Dallas.json"
url_target = url1 + wunderground_api_key + url2
debugging = True

def report_error(arg_tag, arg_parsed_json):
	try:
		error_type = arg_parsed_json['response']['error']['type']
		error_description = arg_parsed_json['response']['error']['description']
		print "*** Parsed JSON error tag=%s, type=%s, description=%s" \
			% (arg_tag, error_type, error_description)
	except KeyError:
		print "*** No error information returned"
	sys.exit(86)

try:
	http_response = urllib2.urlopen(url_target)
except urllib2.URLError, err:
	print "*** Oops, urllib2.urlopen(%s) failed, reason = %s" % (url_target, err.reason)
	sys.exit(86)

http_status = http_response.getcode()
if http_status != 200:
	print "*** Oops, HTTP response code = %d" % (http_status)
	sys.exit(86)

json_response = http_response.read()
if debugging:
	print "HTTP response string:"
	print json_response

parsed_json = json.loads(json_response)
try:
	d_precipi = parsed_json['history']['dailysummary'][00]['precipi']
except KeyError:
	report_error("[history][dailysummary][00][precipi]", parsed_json)
try:
	obs_precipi = parsed_json['history']['observations'][00]['precipi']
except KeyError:
	report_error("[history][observations][00][precipi]", parsed_json)

print "observations & dailysummary = %s %s" % (obs_precipi, d_precipi)
http_response.close()
(Edited)