How to pass dictionary as command line argument to Python script?

后端 未结 4 1831
庸人自扰
庸人自扰 2020-11-27 04:45

How to pass dictionary as command line argument to Python script? I need to get dictionary where key is string and value is list of some elements – for example to look like:

4条回答
  •  离开以前
    2020-11-27 05:06

    Here's another method using stdin. That's the method you want for a json cgi interface (i.e. having a web server pass the request to your script):

    Python:

     import json, sys
     request = json.load( sys.stdin )
    ...
    

    To test your script from a Linux terminal:

    echo '{ "key1": "value 1", "key2": "value 2" }' | python myscript.py
    

    To test your script from a Windows terminal:

    echo { "key1": "value 1", "key2": "value 2" } | python myscript.py
    

提交回复
热议问题