0

This question concerns returning a single piece of data from a json file using python's json module

My json request returns:

{
   "response":{
      "version":"0.1",
      "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
      "features":{
         "forecast":1
      }
   },
   "forecast":{
      "txt_forecast":{
         "date":"1:00 AM BST",
         "forecastday":[
            {
               "period":0,
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "title":"Monday",
               "fcttext":"Mostly cloudy. High of 75F. Winds less than 5 mph.",
               "fcttext_metric":"Mostly cloudy. High of 24C. Winds less than 5 km/h.",
               "pop":"0"
            },
            {
               "period":1,
               "icon":"clear",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif",
               "title":"Monday Night",
               "fcttext":"Clear. Low of 54F. Winds less than 5 mph.",
               "fcttext_metric":"Clear. Low of 12C. Winds less than 5 km/h.",
               "pop":"0"
            },
            {
               "period":2,
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "title":"Tuesday",
               "fcttext":"Clear. High of 70F. Winds less than 5 mph.",
               "fcttext_metric":"Clear. High of 21C. Winds less than 5 km/h.",
               "pop":"0"
            },
            {
               "period":3,
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "title":"Tuesday Night",
               "fcttext":"Mostly cloudy. Low of 52F. Winds less than 5 mph.",
               "fcttext_metric":"Mostly cloudy. Low of 11C. Winds less than 5 km/h.",
               "pop":"0"
            },
            {
               "period":4,
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "title":"Wednesday",
               "fcttext":"Overcast. High of 68F. Winds less than 5 mph.",
               "fcttext_metric":"Overcast. High of 20C. Winds less than 5 km/h.",
               "pop":"0"
            },
            {
               "period":5,
               "icon":"chancerain",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/chancerain.gif",
               "title":"Wednesday Night",
               "fcttext":"Mostly cloudy in the evening, then overcast with a chance of rain. Low of 55F. Winds less than 5 mph. Chance of rain 30%.",
               "fcttext_metric":"Mostly cloudy in the evening, then overcast with a chance of rain. Low of 13C. Winds less than 5 km/h. Chance of rain 30%.",
               "pop":"30"
            },
            {
               "period":6,
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "title":"Thursday",
               "fcttext":"Overcast in the morning, then mostly cloudy. High of 66F. Winds from the East at 5 to 10 mph shifting to the North in the afternoon.",
               "fcttext_metric":"Overcast in the morning, then mostly cloudy. High of 19C. Winds from the East at 10 to 15 km/h shifting to the North in the afternoon.",
               "pop":"0"
            },
            {
               "period":7,
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "title":"Thursday Night",
               "fcttext":"Overcast. Low of 54F. Winds from the East at 5 to 10 mph.",
               "fcttext_metric":"Overcast. Low of 12C. Winds from the East at 5 to 15 km/h.",
               "pop":"0"
            }
         ]
      },
      "simpleforecast":{
         "forecastday":[
            {
               "date":{
                  "epoch":"1379970000",
                  "pretty":"10:00 PM BST on September 23, 2013",
                  "day":23,
                  "month":9,
                  "year":2013,
                  "yday":265,
                  "hour":22,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"September",
                  "weekday_short":"Mon",
                  "weekday":"Monday",
                  "ampm":"PM",
                  "tz_short":"BST",
                  "tz_long":"Europe/London"
               },
               "period":1,
               "high":{
                  "fahrenheit":"75",
                  "celsius":"24"
               },
               "low":{
                  "fahrenheit":"54",
                  "celsius":"12"
               },
               "conditions":"Partly Cloudy",
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "skyicon":"partlycloudy",
               "pop":0,
               "qpf_allday":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_day":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_night":{
                  "in":0.00,
                  "mm":0.0
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":6,
                  "kph":10,
                  "dir":"SE",
                  "degrees":136
               },
               "avewind":{
                  "mph":4,
                  "kph":6,
                  "dir":"SE",
                  "degrees":144
               },
               "avehumidity":81,
               "maxhumidity":85,
               "minhumidity":65
            },
            {
               "date":{
                  "epoch":"1380056400",
                  "pretty":"10:00 PM BST on September 24, 2013",
                  "day":24,
                  "month":9,
                  "year":2013,
                  "yday":266,
                  "hour":22,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"September",
                  "weekday_short":"Tue",
                  "weekday":"Tuesday",
                  "ampm":"PM",
                  "tz_short":"BST",
                  "tz_long":"Europe/London"
               },
               "period":2,
               "high":{
                  "fahrenheit":"70",
                  "celsius":"21"
               },
               "low":{
                  "fahrenheit":"52",
                  "celsius":"11"
               },
               "conditions":"Partly Cloudy",
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "skyicon":"mostlysunny",
               "pop":0,
               "qpf_allday":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_day":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_night":{
                  "in":0.00,
                  "mm":0.0
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":5,
                  "kph":8,
                  "dir":"SE",
                  "degrees":127
               },
               "avewind":{
                  "mph":4,
                  "kph":6,
                  "dir":"SE",
                  "degrees":139
               },
               "avehumidity":79,
               "maxhumidity":92,
               "minhumidity":70
            },
            {
               "date":{
                  "epoch":"1380142800",
                  "pretty":"10:00 PM BST on September 25, 2013",
                  "day":25,
                  "month":9,
                  "year":2013,
                  "yday":267,
                  "hour":22,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"September",
                  "weekday_short":"Wed",
                  "weekday":"Wednesday",
                  "ampm":"PM",
                  "tz_short":"BST",
                  "tz_long":"Europe/London"
               },
               "period":3,
               "high":{
                  "fahrenheit":"68",
                  "celsius":"20"
               },
               "low":{
                  "fahrenheit":"55",
                  "celsius":"13"
               },
               "conditions":"Mostly Cloudy",
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "skyicon":"mostlycloudy",
               "pop":0,
               "qpf_allday":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_day":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_night":{
                  "in":0.06,
                  "mm":1.5
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":3,
                  "kph":5,
                  "dir":"NNE",
                  "degrees":14
               },
               "avewind":{
                  "mph":2,
                  "kph":3,
                  "dir":"East",
                  "degrees":98
               },
               "avehumidity":73,
               "maxhumidity":95,
               "minhumidity":65
            },
            {
               "date":{
                  "epoch":"1380229200",
                  "pretty":"10:00 PM BST on September 26, 2013",
                  "day":26,
                  "month":9,
                  "year":2013,
                  "yday":268,
                  "hour":22,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"September",
                  "weekday_short":"Thu",
                  "weekday":"Thursday",
                  "ampm":"PM",
                  "tz_short":"BST",
                  "tz_long":"Europe/London"
               },
               "period":4,
               "high":{
                  "fahrenheit":"66",
                  "celsius":"19"
               },
               "low":{
                  "fahrenheit":"54",
                  "celsius":"12"
               },
               "conditions":"Mostly Cloudy",
               "icon":"mostlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
               "skyicon":"mostlycloudy",
               "pop":0,
               "qpf_allday":{
                  "in":0.06,
                  "mm":1.5
               },
               "qpf_day":{
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_night":{
                  "in":0.00,
                  "mm":0.0
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":7,
                  "kph":11,
                  "dir":"East",
                  "degrees":87
               },
               "avewind":{
                  "mph":6,
                  "kph":10,
                  "dir":"ESE",
                  "degrees":109
               },
               "avehumidity":81,
               "maxhumidity":91,
               "minhumidity":76
            }
         ]
      }
   }
}

What I want to return is the the 'title' and 'fcttxt' for a specific 'period'(an integer 0-7) from the [forecast][txt_forecast][forecastday] block of the json response I can use the following for loop and print all the entries (for 'period' 0-7).

#!/usr/bin/python

import os, sys, datetime, commands
import urllib2
import json

myweather = json.load(urllib2.urlopen("http://api.wunderground.com/api/API_KEY/forecast/q/27.711,17.994.json"))
myweather_sum = myweather['forecast']['txt_forecast']['forecastday']

for period in myweather_sum:
    myforday = period['title']
    myfctxt = period['fcttext_metric']
    print myforday
    print myfctxt
    print "-----------------------------"

which returns:

Wednesday
Overcast with a chance of rain. High of 19C. Winds from the North at 5 to 20 km/h. Chance of rain 50%.
-----------------------------
Wednesday Night
Overcast with a chance of rain in the evening, then mostly cloudy. Low of 9C. Breezy. Winds from the North at 10 to 20 km/h.
-----------------------------
Thursday
Overcast with a chance of rain. High of 16C. Breezy. Winds from the East at 10 to 20 km/h. Chance of rain 50%.
-----------------------------
Thursday Night
Overcast in the evening, then mostly cloudy. Low of 9C. Winds from the SE at 10 to 15 km/h.
-----------------------------
Friday
Overcast in the morning, then clear. High of 19C. Winds from the SSE at 5 to 15 km/h.
-----------------------------
Friday Night
Overcast in the evening, then partly cloudy. Fog overnight. Low of 7C. Winds from the South at 10 to 15 km/h.
-----------------------------
Saturday
Partly cloudy. High of 17C. Breezy. Winds from the SSE at 10 to 20 km/h.
-----------------------------
Saturday Night
Overcast. Fog overnight. Low of 8C. Winds from the SE at 10 to 15 km/h.

I want to use the 'period'(key) to specify which one of the forecasts is returned.

4

1 回答 1

1

Try this loop, assuming that your desired period is 3:

#UNTESTED
for period in myweather_sum:
    if period['period'] == 3:
        myforday = period['title']
        myfctxt = period['fcttext_metric']
        print myforday
        print myfctxt
于 2013-09-25T15:46:18.140 回答