我有一个url的以下api输出。 我需要从这些'所有'区域'的特定值,但我无法获取它。 你可以在这里建议我需要改变的地方。
dict =[ { "Fabric": "Madgth", "Zones": "rqu_kan23_34334_6666_56713", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:65:36:e4:20:ea" }, { "Fabric": "Madgth", "Zones": "rqu_kan23_C9AE9F10_6666_ytr9", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:69:36:e4:20:ea" }, { "Fabric": "Madgth", "Zones": "sdgh_5672_kan23_asd0", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:62:46:e0:70:bc" }, { "Fabric": "Madgth", "Zones": "fdsf_ytr2_kan23_dsd1", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:6a:46:e0:70:t5" }, { "Fabric": "Madgth", "Zones": "asdasd_5673_kan21_sdg0", "Members": "00:00:00:00:c9:ae:9f:14 40:00:01:63:47:60:04:b2" }, { "Fabric": "Madgth", "Zones": "FGF546_ytr3_kan21_SHQ1", "Members": "00:00:00:00:c9:ae:9f:14 40:00:01:6b:47:60:04:b2" }, { "Fabric": "sdfsdf", "Zones": "rqu_kan23_C9AE9F11_6666_5678", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:60:36:e4:20:ea" }, { "Fabric": "sdfsdf", "Zones": "rqu_kan23_C9AE9F11_6666_ytr12", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:6c:36:e4:20:ea" }, { "Fabric": "sdfsdf", "Zones": "FGF1166_5673_kan23_SHQ0", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:63:46:e0:70:bc" }, { "Fabric": "sdfsdf", "Zones": "FGF1166_ytr3_kan23_SHQ1", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:6b:46:e0:70:bc" }, { "Fabric": "sdfsdf", "Zones": "FGF546_5670_kan21_SHQ0", "Members": "00:00:00:00:c9:ae:9f:15 40:00:01:60:47:60:04:b2" }, { "Fabric": "sdfsdf", "Zones": "FGF546_ytr0_kan21_SHQ1", "Members": "00:00:00:00:c9:ae:9f:15 00:00:00:00:47:60:04:b2" } ]
print dict['Zones']
我似乎得到以下错误。
文件“test.py”,第3行,in print dict ['Zones'] TypeError:元组索引必须是整数,而不是str
答案 0 :(得分:1)
这里只是列表,你必须这样做。
<!----- HTML ----->
<div class="form-radio">
<div class="radio">
<label>
<input type="radio" name="radio" onclick="show();" checked/>
<i class="helper"></i>
I can come
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio" onclick="hide();"/>
<i class="helper"></i>
I cant come
</label>
</div>
</div>
<div class="form-group extra">
<input type="text" required="required" />
<label class="control-label" for="input">Dietary Requirements</label>
<i class="bar"></i>
</div>
<div class="form-group extra">
<input type="text" required="required" />
<label class="control-label" for="input">What song would you like played?</label>
<i class="bar"></i>
</div>
您的示例和错误消息不匹配。作为字典列表给出的示例和错误消息对应于元组。
要使用所有dict[0]['Zones']
"Zones"
请不要将
zones = [i['Zones'] for i in dict if 'Zones' in i]
用作变量名。
评论中的澄清,
dict
答案 1 :(得分:1)
你提出的咒语在声明中有一些错误,这里是固定的错误:
dict =[ { "Fabric": "Madgth", "Zones": "rqu_kan23_34334_6666_56713", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:65:36:e4:20:ea" }, { "Fabric": "Madgth", "Zones": "rqu_kan23_C9AE9F10_6666_ytr9", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:69:36:e4:20:ea" }, { "Fabric": "Madgth", "Zones": "sdgh_5672_kan23_asd0", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:62:46:e0:70:bc" }, { "Fabric": "Madgth", "Zones": "fdsf_ytr2_kan23_dsd1", "Members": "00:00:00:00:c9:ae:9f:10 40:00:01:6a:46:e0:70:t5" }, { "Fabric": "Madgth", "Zones": "asdasd_5673_kan21_sdg0", "Members": "00:00:00:00:c9:ae:9f:14 40:00:01:63:47:60:04:b2" }, { "Fabric": "Madgth", "Zones": "FGF546_ytr3_kan21_SHQ1", "Members": "00:00:00:00:c9:ae:9f:14 40:00:01:6b:47:60:04:b2" }, { "Fabric": "sdfsdf", "zones": "rqu_kan23_C9AE9F11_6666_5678", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:60:36:e4:20:ea" }, { "Fabric": "sdfsdf", "Zones": "rqu_kan23_C9AE9F11_6666_ytr12", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:6c:36:e4:20:ea" }, { "Fabric": "sdfsdf", "Zones": "FGF1166_5673_kan23_SHQ0", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:63:46:e0:70:bc" }, { "Fabric": "sdfsdf", "Zones": "FGF1166_ytr3_kan23_SHQ1", "Members": "00:00:00:00:c9:ae:9f:11 40:00:01:6b:46:e0:70:bc" }, { "Fabric": "sdfsdf", "Zones": "FGF546_5670_kan21_SHQ0", "Members": "00:00:00:00:c9:ae:9f:15 40:00:01:60:47:60:04:b2" }, { "Fabric": "sdfsdf", "Zones": "FGF546_ytr0_kan21_SHQ1", "Members": "00:00:00:00:c9:ae:9f:15 00:00:00:00:47:60:04:b2" } ]
现在这样一个简单的代码就可以了:
list = []
for i in dict:
list.append(i["Zones"])
答案 2 :(得分:1)
您只需循环列表并打印即可。
for item in dict:
print item['Zones']
不建议使用dict
作为变量名称;)