关键错误'超出范围'在Pi上运行Python程序时

时间:2018-02-23 01:07:10

标签: python raspberry-pi keyerror index-error

在下面的图片中......我收到了一个" KeyError"和" IndexError:索引超出范围"在我的Raspberry Pi上运行程序两天。拔掉Pi并将其重新插入以解决问题。什么是导致此问题的潜在问题以及排除故障的好方法?

谢谢!

enter image description here

def UpdateWeather( self ):
        # Use Weather.com for source data.
        cc = 'current_conditions'
        f = 'forecasts'

        # This is where the majic happens. 
        self.w = pywapi.get_weather_from_weather_com( '48085', units='imperial' )
        w = self.w
        try:
            if ( w[cc]['last_updated'] != self.wLastUpdate ):
                self.wLastUpdate = w[cc]['last_updated']
                print "New Weather Update: " + self.wLastUpdate
                self.temp = string.lower( w[cc]['temperature'] )
                self.feels_like = string.lower( w[cc]['feels_like'] )
                self.wind_speed = string.lower( w[cc]['wind']['speed'] )
                self.baro = string.lower( w[cc]['barometer']['reading'] )
                self.wind_dir = string.upper( w[cc]['wind']['text'] )
                self.humid = string.upper( w[cc]['humidity'] )
                self.vis = string.upper( w[cc]['visibility'] )
                self.gust = string.upper( w[cc]['wind']['gust'] )
                self.wind_direction = string.upper( w[cc]['wind']['direction'] )
                self.day[0] = w[f][0]['day_of_week']
                self.day[1] = w[f][1]['day_of_week']
                self.day[2] = w[f][2]['day_of_week']
                self.day[3] = w[f][3]['day_of_week']
                self.sunrise = w[f][0]['sunrise']
                self.sunset = w[f][0]['sunset']
                self.icon[0] = getIcon( w, 0 )
                self.icon[1] = getIcon( w, 1 )
                self.icon[2] = getIcon( w, 2 )
                self.icon[3] = getIcon( w, 3 )
                print 'Icon Index: ', self.icon[0], self.icon[1], self.icon[2], self.icon[3]
                #print 'File: ', sd+icons[self.icon[0]]
                self.rain[0] = w[f][0]['day']['chance_precip']
                self.rain[1] = w[f][1]['day']['chance_precip']
                self.rain[2] = w[f][2]['day']['chance_precip']
                self.rain[3] = w[f][3]['day']['chance_precip']
                if ( w[f][0]['high'] == 'N/A' ):
                    self.temps[0][0] = '--'
                else:   
                    self.temps[0][0] = w[f][0]['high'] + unichr(0x2109)
                self.temps[0][1] = w[f][0]['low'] + unichr(0x2109)
                self.temps[1][0] = w[f][1]['high'] + unichr(0x2109)
                self.temps[1][1] = w[f][1]['low'] + unichr(0x2109)
                self.temps[2][0] = w[f][2]['high'] + unichr(0x2109)
                self.temps[2][1] = w[f][2]['low'] + unichr(0x2109)
                self.temps[3][0] = w[f][3]['high'] + unichr(0x2109)
                self.temps[3][1] = w[f][3]['low'] + unichr(0x2109)
        except KeyError:
            print "KeyError -> Weather Error"
            self.temp = '??'
            self.wLastUpdate = ''
            return False
        #except ValueError:
        #   print "ValueError -> Weather Error"

        return True

0 个答案:

没有答案