Search code examples
pythondictionaryunixcommand-line-argumentssys

Passing dictionary arguments through command line and accessing them in a python script


I am trying to pass a dictionary consisting of arguments to a python script and later use that dictionary in the script. I am saving the dictionary in the environment variable of airflow.

For example, the script should run on the command line as, python test.py dict_name

The dict_name looks like this,

{ "path": "firstlevel/secondlevel", "name": "xyz", "key": "12345" }

And, I need to use the keys of above dictionary in place of the actual values in my python script i.e. test.py

I tried using data = sys.argv[1] in the init function of a class. But, it gives me a list instead of dictionarly. And that way, using features of dictionary is not possible.

def __init__(self, data = sys.argv[1]):
    print data

I am instantiating the object like this,

f = class_name(sys.argv[1:])

The output I get is,

['path', ':', 'firstlevel/secondlevel', 'name', ':', 'xyz', 'key', ':', '12345']

My objective is to get the data as a dictionary and not a list in python script because I want to replace the values on my python script by keys only.

Here is the usecase to explain my point in a clearer way.

fun(name = 'xyz', key = 12345)

To

fun(sys.argv['name'], sys.argv['key'])

Traceback

Traceback (most recent call last): File "test.py", line 13, in objec_name= class_name(sys.argv[1:]) File "test.py", line 6, in init data = json.loads(sys.argv[1]) File "C:\Users\Sarvesh Pandey\AppData\Local\Programs\Python\Python38\lib\json_init_.py", line 357, in loads return _default_decoder.decode(s) File "C:\Users\Sarvesh Pandey\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())


Solution

  • JSON - or JavaScript Object Representation is one of the way of taking Python objects and converting them into a string-like representation, suitable for passing around to multiple languages.

    python saver.py '{"names": ["J.J.", "April"], "years": [25, 29]}'
    

    In your python script, do this:

    import json
    data=json.loads(argv[1])
    

    This will give you back a dictionary representing the data you wanted to pass in.

    You can then apply to the function as you intended.

    fun(data['name'], data['key'])
    

    Edit: You also need to consider the parsing issue

    E.g.

    if you run print sys.argv[1] you probably get '{favorited: which the json module cannot decode into a json object.

    try escaping your inner quotes so it is passed as 1 argument like so:

    "{"\""favorited"\"": false, "\""contributors"\"": null}"