Assuming my json response looks like this:
{
"classifier_id" : "12345",
"url" : "https://something.com",
"text" : "text",
"top_class" : "class1",
"classes" : [ {
"class_name" : "car",
"confidence" : 0.9862312904583641
}, {
"class_name" : "bus",
"confidence" : 0.013768709541636032
} ]
}
with the underlying GET request being
callIBM = requests.get(
url="https://something.com",
params={
"text": update.message.text,
},
headers={
"Authorization": "XXX",
},
)
and I want to parse the result in python.
I tried:
ibmscore1 = '{}'.format(callIBM.json()["classes"][0]["class_name"]["confidence"])
ibmscore2 = '{}'.format(callIBM.json()["classes"][1]["class_name"]["confidence"])
but it's not working. Could one kindly assist a python newby?
You are trying to access confidence
inside class_name
but class_name
is not a dictionary.
Try:
resp = callIBM.json()
class1, conf1 = resp["classes"][0]["class_name"], resp["classes"][0]["confidence"]
class2, conf2 = resp["classes"][1]["class_name"], resp["classes"][0]["confidence"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.