Python 列表索引必须是整数错误
我的Python代码中出现以下错误:Python 列表索引必须是整数错误,python,Python,我的Python代码中出现以下错误: if data['result'] == 0: TypeError: list indices must be integers, not str 代码如下: data = urllib.urlencode(parameters) req = urllib2.Request(url, data) try: response = urllib2.urlopen(req) except urllib2.URLError, e: self.red
if data['result'] == 0:
TypeError: list indices must be integers, not str
代码如下:
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
self.redirect('/error')
json_post = response.read()
data = json.loads(json_post)
response_dict = simplejson.loads(json_post)
virustotal = VirusTotal()
if data['result'] == 0:
virustotal_result = True
elif data['result'] == -2:
self.response.out.write("API Request Rate Limit Exceeded<br/>")
elif data['result'] == -1:
self.response.out.write("API Key provided is wrong<br/>")
elif data['result'] == 1:
self.response.out.write("Time Stamp : %s<br/>"% data['report'][0])
data=urllib.urlencode(参数)
请求(url,数据)
尝试:
响应=urllib2.urlopen(请求)
除urllib2.URLError外,e:
self.redirect(“/error”)
json_post=response.read()
data=json.loads(json_post)
response\u dict=simplejson.load(json\u post)
virustotal=virustotal()
如果数据['result']==0:
virustotal_结果=真
elif数据['result']==-2:
self.response.out.write(“超出API请求速率限制
”)
elif数据['result']==-1:
self.response.out.write(“提供的API密钥错误
”)
elif数据['result']==1:
self.response.out.write(“时间戳:%s
%data['report'][0])
我知道数据是一个列表。所以我也把它改成了整数,但是代码显示了范围错误。请帮助???当你说
data=json.loads(json\u post)
时,听起来你得到的是一个列表,而不是你期望的目录
如果这不是问题所在,请尝试使用完整回溯和
json\u post
的值进行更新,您将从响应中获得一个对象数组。读取
,这意味着您将获得一个Python列表
。试试这个:
data = json.loads(json_post)[0]
我是python新手,我读过dicts和list,但现在我不知道如何将其转换为整数索引。答案取决于对现有数据的理解。考虑阅读以了解Python数据类型。如果打印“代码>数据< /代码>,会得到什么?