Created
July 21, 2015 07:23
-
-
Save ryosan-470/fe2f145899a1eb913241 to your computer and use it in GitHub Desktop.
Sample using json in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?q=" | |
def kelbin2degree(d): | |
return d - 273.15 | |
def get_weather(city): | |
with urllib.request.urlopen(BASE_URL + city) as response: | |
html = response.read().decode('utf-8') | |
return json.loads(html) | |
def main(): | |
city = input(">> ") | |
result = get_weather(city) | |
status = int(result["cod"]) | |
if status == 200: | |
temp_min = kelbin2degree(result['main']['temp_min']) | |
temp_max = kelbin2degree(result['main']['temp_max']) | |
get_city = result['name'] | |
print("City:{} Temp MAX:{} MIN:{}".format(get_city, temp_min, temp_max)) | |
else: | |
print("Sorry, Your input city is nothing") | |
main() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment