在下面嵌套的字典中,我希望術語出現在“field_name”字串中的“fields”值中。
response = {
"indices": [
"device",
],
"fields": {
"hostname": {
"text": {
"type": "text",
"searchable": true,
"aggregatable": false
}
}
}
}
我的代碼-
for field in response['fields']:
if field == field_name:
print(field)
d_type= response["fields"][field_name]["text"]["type"]
print(d_type)
錯誤-
d_type= response["fields"][field_name]["text"]["type"]
KeyError: 'text
預期輸出-
fieldname=hostname
d_type=text
uj5u.com熱心網友回復:
嘗試使用這個
field_name = 'hostname'
for field in response['fields']:
if field == field_name:
d_type = response['fields'][field]['text']['type']
print('field=' field_name)
print('d_type=' d_type)
field- 字典鍵
如果field_name是“主機名”,則response['fields'][field]在回圈內獲取字典:
"text": {
"type": "text",
"searchable": True,
"aggregatable": False
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429008.html
