I am trying to parse json with deep nesting, up to 10 elements, how to handle an exception when there is no key in the dictionary?
d = json.loads(file) print( d['date'], d['тип'], d['инн'], d['кпп'], d['огрн'], d['имяПолное'], d['имяКраткое'], d['оснВидДеятельности']['код'], d['оснВидДеятельности']['наим'], d['свАдрес']['адресРФ']['кодАдрКладр'] ) For example, there is no last element (maybe in another file), I get an error:
d['свАдрес']['адресРФ']['кодАдрКлад'] KeyError: 'кодАдрКлад' I wanted to write a handler, but it didn’t work out that much, the error is the same
def get_err(s): try: return s except: return '' In print, I show everything for debugging, ideally you need to collect the key values into a string with a separator; for subsequent import to the database