Hope you are having a great day! I am trying to return {'green color','road','street','yellow color'} which I was able to do with this function in python 3 but I only want it to return it if the key "CLASSES" exists in the dictionary "json_data". When I run the function with the following JSON code it works but when I run other code that does not have a key "CLASSES" I just want it to return None. How do I do that? When I give it other json data right now it shows the image at the bottom.
Here is the code:
def parse_classes(json_data):
lst = list()
for classes in json_data['images'][0]['classifiers'][0]:
for item in json_data['images'][0]['classifiers'][0]['classes']:
class1 = item['class']
lst.append(class1)
else:
print("")
lst = set(lst)
return(lst)
Here is the json:
{
"custom_classes": 0,
"images": [
{
"classifiers": [
{
"classes": [
{
"class": "street",
"score": 0.846,
"type_hierarchy": "/road/street"
},
{
"class": "road",
"score": 0.85
},
{
"class": "yellow color",
"score": 0.872
},
{
"class": "green color",
"score": 0.702
}
],
"classifier_id": "default",
"name": "default"
}
],
"image": "images/parisstreets/paris-streets-1.jpg"
}
],
"images_processed": 1
}
Image:
Thank you!
UPDATE: This is a followup question to Retrieve Multiple JSON Items Python
Aucun commentaire:
Enregistrer un commentaire