How to pass an entire list as command line argument in Python?

前端 未结 7 1833
滥情空心
滥情空心 2020-12-02 16:59

I was trying to pass two lists containing integers as arguments to a python code. But sys.argv[i] gets the parameters as a list of string.

Input would

7条回答
  •  再見小時候
    2020-12-02 17:55

    You can also do the following:

    say, you have foo.py :

    import json
    import sys
    data = json.loads(sys.argv[1])
    print data, type(data)
    

    Then if you run the above as : python foo.py "[1,2,3]"

    Output:

    [1, 2, 3]

提交回复
热议问题