我正在执行一个get请求(json输出):
url = "https://"+user+":"+api+"@api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/"+id+"/getSoftwareComponents"
querystring = {"objectMask":"mask[passwords]"}
headers = {
'cache-control': "no-cache"
}
response = requests.get( url, headers=headers, params=querystring)
pwdata = response.json()
print (json.dumps(pwdata, indent=2))
输出:
[
{
"manufacturerLicenseInstance": "",
"passwords": [
{
"username": "test",
"modifyDate": "2018-05-11T21:52:54+09:00",
"softwareId": 36440389,
"id": 38400901,
"port": null,
"password": "xxxxxxxx",
"createDate": "2018-05-11T21:52:54+09:00",
"software": null
}
],
"hardwareId": null,
"id": 36440389
},
{
"manufacturerLicenseInstance": "",
"passwords": [
{
"username": "nabe",
"modifyDate": "2018-05-11T21:52:54+09:00",
"softwareId": 36440391,
"id": 38402045,
"port": null,
"password": "xxxxxxx",
"createDate": "2018-05-11T21:52:54+09:00",
"software": null
}
],
"hardwareId": null,
"id": 36440391
}
]
从上面的输出中,我尝试用以下方法过滤id
中的passwords[]
值:
pwdid = [pwd['id'] for pwd in pwdata['passwords']]
或者
pwdid = pwdata['passwords'][4]['id']
它们都给出了list indices must be integers, not str
错误
显然,我使用listpwd['id']
的方式有些不正确。你知道吗
板球是对的,你有一个错误,因为你用一个字符串子集列表。试试这个:
从“数据”中提取密码ID
输出
易于阅读的版本
输出
相关问题 更多 >
编程相关推荐