在使用Elasticsearch的Python中遇到"exceptions.TypeError: list indices must be integers not str
我想用我的函数获取文档的ID,但遇到了这个错误:
var = data['hits']['hits']['_id']
exceptions.TypeError: list indices must be integers, not str
这是我写的小函数:
def FoundIdDocument(reference):
print "foundiddocument"
url = BuildUrl()+'_search?q=name:"'+reference.replace(' ','%20')+'"'
req = urllib2.Request(url)
out = urllib2.urlopen(req)
data = out.read()
print data
# returned data is JSON
data = json.loads(data)
# total number of results
var = data['hits']['hits']['_id']
print var
1 个回答
4
打印出这些键,然后自己想办法理解:
print data.keys()
# Does it have 'hits'? If yes, do this:
print data['hits'].keys()
# Does it have 'hits'? If yes, do this:
print data['hits']['hits'].keys()
# You should have hit an error by this point