Query Temperature from wunderground.com using python

Query the Temperature from a specific location using python.
As this example, you would require to have an account and activate your API key from

import urllib2
import json
f = urllib2.urlopen('http://api.wunderground.com/api/YOUR_API_KEY/geolookup/conditions/q/IA/Cedar_Rapids.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
Cel = '%.2f' % ((temp_f -32) * 5.0/9.0)

print "Current temperature in %s is: %s" % (location, Cel), "C. "
f.close()

Current Result is :

Current temperature in YOUR_CITY is: 12.61 C.

Conversion of the temp from Fahrenheit to Celsius:

Cel = (Fahrenheit - 32) * 5.0/9.0

Limit digit after the comma.
2f for 2 digit;
4f for 4 digits.

'%.2f' %
So:
Cel = '%.2f' % ((temp_f -32) * 5.0/9.0)

While still looking at the json info; I have seen that if you need the celsius:
replace this line and remove the conversion 🙂

temp_f = parsed_json['current_observation']['temp_c']

Which makes now:

import urllib2
import json
f = urllib2.urlopen('http://api.wunderground.com/api/YOUR_API_KEY/geolookup/conditions/q/IA/Cedar_Rapids.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_c = parsed_json['current_observation']['temp_c']

print "Current temperature in %s is: %s" % (location, temp_c), "C. "
f.close()

Have fun.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

Menu