Python 如何在字典中搜索值?

Python 如何在字典中搜索值?,python,Python,我正在为python制作一个开源的twitch.tv API包装器,到目前为止我已经: import urllib2 import json import time waittime = 1 baseurl = 'https://api.twitch.tv/kraken/' class twitchchannelinfo(): def __init__ (self,channel): self.channel = channel time.sleep(w

我正在为python制作一个开源的twitch.tv API包装器,到目前为止我已经:

import urllib2
import json
import time
waittime = 1
baseurl = 'https://api.twitch.tv/kraken/'

class twitchchannelinfo():
    def __init__ (self,channel):
        self.channel = channel
        time.sleep(waittime)
        self.dict1 = json.loads(urllib2.urlopen(baseurl + 'channels/' + channel).read())

    def getstatus(self):
        return self.dict1 ['status']
    def getdisplay_name(self):
        return self.dict1 ['display_name']
    def getmature(self):
        return self.dict1 ['mature']
    def getchanurl(self):
        return self.dict1 ['url']
    def getcreated_at(self):
        return self.dict1 ['created_at']
    def getteams(self):
        return self.dict1 ['teams']
    def getgame(self):
        return self.dict1 ['game']
    def getupdated_at(self):
        return self.dict1 ['updated_at']
我想在这个API中添加错误检查。对于错误,服务器将返回如下json响应:

{
    "error": "Unprocessable Entity",
    "status": 422,
    "message": "Channel 'deeman' is not available on Twitch"
}
然后我使用
json.loads将其转换为字典。我如何检查字典中的“error”值,或者有更好的方法吗

if 'error' in self.dict1:
  # do blah

这只是使用try…的另一种方法,但我建议执行以下操作:

if 'error' in self.dict1:
    raise ValueError("%s: %s" % (self.dict1["error"], self.dict1["message"]))
if 'error' in self.dict1:
    raise ValueError("%s: %s" % (self.dict1["error"], self.dict1["message"]))