File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Chapter14–WorkingwithCSVFilesandJSONData Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/python3.5
2+
3+
4+ """
5+
6+ Author: Samrat Banerjee
7+ Dated: 08/09/2018
8+ Description: Project: Prints the weather for a location from the command line.
9+
10+ """
11+
12+ import json , requests , sys
13+
14+ # Compute location from command line arguments.
15+ if len (sys .argv ) < 2 :
16+ print ('Usage: quickWeather.py location' )
17+ sys .exit ()
18+ location = ' ' .join (sys .argv [1 :])
19+
20+ # Download the JSON data from OpenWeatherMap.org's API.
21+ url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3' % (location )
22+ response = requests .get (url )
23+ response .raise_for_status ()
24+
25+ # Load JSON data into a Python variable.
26+ weatherData = json .loads (response .text )
27+
28+ # Print weather descriptions.
29+ w = weatherData ['list' ]
30+ print ('Current weather in %s:' % (location ))
31+ print (w [0 ]['weather' ][0 ]['main' ], '-' , w [0 ]['weather' ][0 ]['description' ])
32+ print ()
33+ print ('Tomorrow:' )
34+ print (w [1 ]['weather' ][0 ]['main' ], '-' , w [1 ]['weather' ][0 ]['description' ])
35+ print ()
36+ print ('Day after tomorrow:' )
37+ print (w [2 ]['weather' ][0 ]['main' ], '-' , w [2 ]['weather' ][0 ]['description' ])
You can’t perform that action at this time.
0 commit comments