Json and Python : How to treat subitems when item is empty

57 views Asked by At

I'm not an expert in python. I load json files to collect items and subitems and write some HTML expression like : text= "<h1>Level 1 :"+[item_i]+"<h2>Level 2 :"+[item_i][subitem_i]+"

When item_i is empty, I get of course an error because [item_i][subitem_i] does not exist. To solve that, I've defined a function including an If Then condition, which tests the length of each item, writes text= "<h1>Level 1 :"Absent" if the length is = 0, and writes the full expression otherwise.

This not a pretty solution. and the code is more difficult to read and maintain. Is there a more direct solution to deal this issue ?

Thank you Villard

0

There are 0 answers