I am trying to get values from a JSON file and store it in a python Dictionary.
I want to assign the values for what ever is available from the keys I give and for those which are not available I want to store as "None"
I tried the following,
try:
dataDict['syn']= results['synonyms']
dataDict['reactivity']= results['crossReactivity']
dataDict['Temp']= results['storageTemp']
dataDict['catNum']= results['catalogNum']
dataDict['shipping']= results['shippingInstructions']
except KeyError as e:
dataDict[e.args[0]] = "None"
sa = f"""
synonyms: {dataDict['syn']}
crossReactivity: {dataDict['reactivity']}
storageTemp: {dataDict['Temp']}
catalogNum: {dataDict['catNum']}
shippingInstructions: {dataDict['shipping']}
"""
print (sa)
I get the following output,
synonyms: None
crossReactivity:
storageTemp:
catalogNum:
shippingInstructions:
If 'synonyms' is not available on the JSON file 'None' is assigned to dataDict['syn'] but where as the rest of the statements are getting ignored.
How to execute the rest of the assignments without having to write each and everything in separate try and except?