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

前端 未结 7 1788
滥情空心
滥情空心 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:35

    No, there is no way pass a list in a command line argument. Command line arguments are always string. But there is a better way to convert it to list. You can do it like that:

    import ast
    
    A = ast.literal_eval(strA)
    B = ast.literal_eval(strB)
    
    0 讨论(0)
  • 2020-12-02 17:49

    Why not:

    python foo.py 1,2,3,4 5,6,7,8  
    

    Much cleaner than trying to eval python and doesn't require your user to know python format.

    import sys
    
    list1 = sys.argv[1].split(',')
    list2 = [int(c) for c in sys.argv[2].split(',')]  # if you want ints
    
    0 讨论(0)
  • 2020-12-02 17:53

    Command line arguments are always passed as strings. You will need to parse them into your required data type yourself.

    >>> input = "[2,3,4,5]"
    >>> map(float, input.strip('[]').split(','))
    [2.0, 3.0, 4.0, 5.0]
    >>> A = map(float, input.strip('[]').split(','))
    >>> print(A, type(A))
    ([2.0, 3.0, 4.0, 5.0], <type 'list'>)
    

    There are libraries like argparse and click that let you define your own argument type conversion but argparse treats "[2,3,4]" the same as [ 2 , 3 , 4 ] so I doubt it will be useful.

    edit Jan 2019 This answer seems to get a bit of action still so I'll add another option taken directly from the argparse docs.

    You can use action=append to allow repeated arguments to be collected into a single list.

    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument('--foo', action='append')
    >>> parser.parse_args('--foo 1 --foo 2'.split())
    Namespace(foo=['1', '2'])
    

    In this case you would pass --foo ? once for each list item. Using OPs example: python filename.py --foo 2 --foo 3 --foo 4 --foo 5 would result in foo=[2,3,4,5]

    0 讨论(0)
  • 2020-12-02 17:55

    I tested this on my end, and my input looks like this:

    python foo.py "[1,2,3,4]" "[5,6,7,8,9]"
    

    I'm doing the following to convert the two params of interest:

    import ast
    import sys
    
    list1 = ast.literal_eval(sys.argv[1])
    list2 = ast.literal_eval(sys.argv[2])
    
    0 讨论(0)
  • 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] <type 'list'>

    0 讨论(0)
  • 2020-12-02 17:56

    You have to escape:

    python some.py \[2,3,4,5\] \[1,2,3,4\]
    

    some.py

    import sys
    
    print sys.argv[1]
    print sys.argv[2]
    

    this gives me:

    [2,3,4,5]
    [1,2,3,4]
    

    UPDATE:

    import sys
    import ast
    
    d = ast.literal_eval(sys.argv[1])
    b = ast.literal_eval(sys.argv[2])
    
    for a in d:
        print a
    
    for e in b:
        print e
    

    first will give:

    2
    3
    4
    5
    

    and second will give

    1
    2
    3
    4
    
    0 讨论(0)
提交回复
热议问题