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

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


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 jsondata=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}"