Answer» Hi, SORRY for the noob question. I've just (re)started trying to understand Python, and decided to start with trying to make a script to create a personalized weather report. I came across this and imported it into my code. I followed the suggestions on the api's page and set
Code: [Select]yahoo_result = pywapi.get_weather_from_yahoo('UKXX0001', 'metric') Printing that gives a slightly less cleanly formatted version of what's below, which lead me to believe it was storing the information in a dictionary.
Code: [Select]{ 'astronomy': {'sunrise': '6:03 am', 'sunset': '8:12 pm'}, 'atmosphere': { 'humidity': '64', 'pressure': '982.05', 'rising': '0', 'visibility': '9.99'}, 'condition': { 'code': '30', 'date': 'Thu, 29 Aug 2013 3:50 pm BST', 'temp': '19', 'text': 'Partly Cloudy', 'title': 'Conditions for Aberdeen, UK at 3:50 pm BST'}, 'forecasts': [ { 'code': '29', 'date': '29 Aug 2013', 'day': 'Thu', 'high': '18', 'LOW': '10', 'text': 'Partly Cloudy'}, { 'code': '30', 'date': '30 Aug 2013', 'day': 'Fri', 'high': '19', 'low': '7', 'text': 'Partly Cloudy'}, { 'code': '30', 'date': '31 Aug 2013', 'day': 'Sat', 'high': '16', 'low': '10', 'text': 'Partly Cloudy'}, { 'code': '26', 'date': '1 SEP 2013', 'day': 'Sun', 'high': '16', 'low': '11', 'text': 'Cloudy'}, { 'code': '39', 'date': '2 Sep 2013', 'day': 'Mon', 'high': '18', 'low': '12', 'text': 'SCATTERED Showers'}], 'geo': {'lat': '57.13', 'long': '-2.09'}, 'html_description': '\n<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />\n<b>Current Conditions:</b><br />\nPartly Cloudy, 19 C<BR />\n<BR /><b>Forecast:</b><BR />\nThu - Partly Cloudy. High: 18 Low: 10<br />\nFri - Partly Cloudy. High: 19 Low: 7<br />\nSat - Partly Cloudy. High: 16 Low: 10<br />\nSun - Cloudy. High: 16 Low: 11<br />\nMon - Scattered Showers. High: 18 Low: 12<br />\n<br />\n<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Aberdeen__UK/*http://weather.yahoo.com/forecast/UKXX0001_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>\n', 'link': 'http://us.rd.yahoo.com/dailynews/rss/weather/Aberdeen__UK/*http://weather.yahoo.com/forecast/UKXX0001_c.html', 'location': {'city': 'Aberdeen', 'country': 'UK', 'region': ''}, 'title': 'Yahoo! Weather - Aberdeen, UK', 'units': { 'distance': 'km', 'pressure': 'mb', 'speed': 'km/h', 'temperature': 'C'}, 'wind': {'chill': '19', 'direction': '0', 'speed': '3.22'}}
To be able to refer to SPECIFIC attributes within the list, it was suggested to me by a friend that I try something similar to:
Code: [Select]print(yahoo_result.astronomy.sunrise) but then I get the error:
Code: [Select]Traceback (most recent call last): File "C:\Users\James\weather.py", line 10, in <module> print(result.astronomy.sunrise) AttributeError: 'dict' object has no attribute 'astronomy' Whole code being:
Code: [Select]#!/usr/bin/env python import pywapi
yahoo_result = pywapi.get_weather_from_yahoo('UKXX0001', 'metric') print(yahoo_result.astronomy.sunrise)
and whole response being:
Code: [Select]{ 'astronomy': {'sunrise': '6:03 am', 'sunset': '8:12 pm'}, 'atmosphere': { 'humidity': '64', 'pressure': '982.05', 'rising': '0', 'visibility': '9.99'}, 'condition': { 'code': '30', 'date': 'Thu, 29 Aug 2013 3:50 pm BST', 'temp': '19', 'text': 'Partly Cloudy', 'title': 'Conditions for Aberdeen, UK at 3:50 pm BST'}, 'forecasts': [ { 'code': '29', 'date': '29 Aug 2013', 'day': 'Thu', 'high': '18', 'low': '10', 'text': 'Partly Cloudy'}, { 'code': '30', 'date': '30 Aug 2013', 'day': 'Fri', 'high': '19', 'low': '7', 'text': 'Partly Cloudy'}, { 'code': '30', 'date': '31 Aug 2013', 'day': 'Sat', 'high': '16', 'low': '10', 'text': 'Partly Cloudy'}, { 'code': '26', 'date': '1 Sep 2013', 'day': 'Sun', 'high': '16', 'low': '11', 'text': 'Cloudy'}, { 'code': '39', 'date': '2 Sep 2013', 'day': 'Mon', 'high': '18', 'low': '12', 'text': 'Scattered Showers'}], 'geo': {'lat': '57.13', 'long': '-2.09'}, 'html_description': '\n<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />\n<b>Current Conditions:</b><br />\nPartly Cloudy, 19 C<BR />\n<BR /><b>Forecast:</b><BR />\nThu - Partly Cloudy. High: 18 Low: 10<br />\nFri - Partly Cloudy. High: 19 Low: 7<br />\nSat - Partly Cloudy. High: 16 Low: 10<br />\nSun - Cloudy. High: 16 Low: 11<br />\nMon - Scattered Showers. High: 18 Low: 12<br />\n<br />\n<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Aberdeen__UK/*http://weather.yahoo.com/forecast/UKXX0001_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>\n', 'link': 'http://us.rd.yahoo.com/dailynews/rss/weather/Aberdeen__UK/*http://weather.yahoo.com/forecast/UKXX0001_c.html', 'location': {'city': 'Aberdeen', 'country': 'UK', 'region': ''}, 'title': 'Yahoo! Weather - Aberdeen, UK', 'units': { 'distance': 'km', 'pressure': 'mb', 'speed': 'km/h', 'temperature': 'C'}, 'wind': {'chill': '19', 'direction': '0', 'speed': '3.22'}} Traceback (most recent call last): File "C:\Users\James\weather.py", line 10, in <module> print(result.astronomy.sunrise) AttributeError: 'dict' object has no attribute 'astronomy'
If possible, could someone explain to me what I'm doing wrong? Thanks in advance, James Python dictionaries are indexed with square brackets in most cases:
Code: [Select]print(yahoo_result["astronomy"]["sunrise"])
Ought to be what you are after.That was it!
Thanks Hi, sorry for double posting.
If I wanted to convert one of the variables to lowercase, how would I go about doing it?
Code: [Select]print("The conditions will be " + (yahoo_result["condition"]["text"]).lower + ".")My guess would have been something like that, but I get the error:
Code: [Select] File "C:\Users\James\weather.py", line 9, in fullweather print("The conditions will be " + (yahoo_result["condition"]["text"]).lower + ".") TypeError: Can't convert 'builtin_function_or_method' object to str implicitly Thanks againYou are referring to the lower method itself, rather than calling it. When you do .method without a pair of parentheses, you are actually referring directly to the function itself. In order to call a function you need a parameter list- in this case, the lower method doesn't take any parameters, so you can use an empty set of parentheses, like so:
Code: [Select]print("The conditions will be " + (yahoo_result["condition"]["text"]).lower() + ".")
That should fix that problem.Thanks
Code: [Select] elif (isistance(timearg, int)) == true: Sorry for this again, but is there a way to elif when the variable is an integer? Thanks
Code: [Select] elif type(timearg) is int:
|