"exceptions.TypeError: list indices must be integers not str" python 中的 Elasticsearch



我尝试用我的函数获取文档的id,但我得到了这个错误:

    var = data['hits']['hits']['_id']
exceptions.TypeError: list indices must be integers, not str

my little function:

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

打印密钥并计算:

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

相关内容

最新更新