How to execute many variable assignments from JSON and if key error assign None and execute rest of all in python

Guru

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?

Maurice Meyer

You can use .get(), which takes a 'default' value:

dataDict = {}
results = {"synonyms": "foo", "storageTemp": 15}

dataDict['syn'] = results.get('synonyms', None)
dataDict['reactivity'] = results.get('crossReactivity', None)
dataDict['Temp'] = results.get('storageTemp', None)
dataDict['catNum'] = results.get('catalogNum', None)
dataDict['shipping'] = results.get('shippingInstructions', None)

print(dataDict)

Out:

{'shipping': None, 'catNum': None, 'reactivity': None, 'syn': 'foo', 'Temp': 15}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to assign the variable to the argument in execute process task?

Python: How many objects were created in variable assignments?

How to read all data from cursor.execute() in python?

Syntax error while using EXECUTE format in Postgresql Function to assign variable

How do I assign a value to a JSON key using a variable in Python?

bash script, execute shell from another shell and assign results to a variable

How to execute a file from python

How to execute psql from UNIX with variable for predicates?

How to execute code from a string variable in Crystal?

How to execute a sql resultset from a table variable

getting error with execute many in python when i tried to connect to DB

How to assign key from button to a state variable

How to execute Python files from Python?

Python, how to execute a line of code without it stopping the rest of the code from executing?

How to execute a command in the terminal from a Python script?

How to execute Python inline from a bash shell

How to execute Python script from Java?

How to execute a Python script from the Django shell?

How to execute python script from php?

How to execute MonkeyRunner from Yii webpage and Python

How to execute multiple commands on Terminal, from Python

How to execute the rest of the php script after json_encode output?

Error in dynamic execute with local variable

SQL DUPLICATE KEY UPDATE Execute many fails with not all arguments converted during string formatting

Execute python file from another python file by passing argument as variable

Iccube Rest API, how to execute a ExecuteMdxScript from C#?

How can I assign variable from json?

How to assign a value from JSON to a variable in javascript

How To Execute Python REST API test suite on AWS Lambda