Se riesco a rilevare uno KeyError
, come posso dire quale ricerca non è riuscita?Quale chiave non è riuscita in Python KeyError?
def poijson2xml(location_node, POI_JSON):
try:
man_json = POI_JSON["FastestMan"]
woman_json = POI_JSON["FastestWoman"]
except KeyError:
# How can I tell what key ("FastestMan" or "FastestWoman") caused the error?
LogErrorMessage ("POIJSON2XML", "Can't find mandatory key in JSON")
Dal momento che si dovrebbe eseguire il branching sulla chiave che ha comunque fallito, è probabilmente più chiaro inserire ogni ricerca in una dichiarazione 'try' separata. – chepner