How to pass a complete list as a command line argument in Python?

I tried to pass two lists with integers as arguments to python code. But sys.argv[i] gets the parameters as a list of strings.

The input will look like this:

 $ python filename.py [2,3,4,5] [1,2,3,4] 

I found the following hack to convert a list.

 strA = sys.argv[1].replace('[', ' ').replace(']', ' ').replace(',', ' ').split() strB = sys.argv[2].replace('[', ' ').replace(']', ' ').replace(',', ' ').split() A = [float(i) for i in strA] B = [float (i) for i in strB] 

Is there a better way to do this?

+11
source share
7 answers

Command line arguments are always passed as strings. You will need to parse them into the desired 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'>) 

These are libraries like argparse and click that allow you to define your own type conversion of argument types, but argparse treats "[2,3,4]" same as [ 2 , 3 , 4 ] so I doubt it would be useful.

edit January 2019 This seems to have worked a bit more, so I will add another option, taken directly from the argparse documentation.

You can use action=append to allow duplicate arguments in 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, will you pass --foo? once for each item in the list. OP usage example: python filename.py --foo 2 --foo 3 --foo 4 --foo 5 will result in foo=[2,3,4,5]

+7
source

Do not reinvent the wheel. Use the argparse module , be explicit and pass the actual parameter lists

 import argparse # defined command line options # this also generates --help and error handling CLI=argparse.ArgumentParser() CLI.add_argument( "--lista", # name on the CLI - drop the '--' for positional/required parameters nargs="*", # 0 or more values expected => creates a list type=int, default=[1, 2, 3], # default if nothing is provided ) CLI.add_argument( "--listb", nargs="*", type=float, # any type/callable can be used here default=[], ) # parse the command line args = CLI.parse_args() # access CLI options print("lista: %r" % args.lista) print("listb: %r" % args.listb) 

You can call using

 $ python my_app.py --listb 5 6 7 8 --lista 1 2 3 4 lista: [1, 2, 3, 4] listb: [5.0, 6.0, 7.0, 8.0] 
+26
source

I tested this at my end and my input looks like this:

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

I am doing the following to convert two parameters of interest:

 import ast import sys list1 = ast.literal_eval(sys.argv[1]) list2 = ast.literal_eval(sys.argv[2]) 
+7
source

Why not:

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

It is much cleaner than trying to evaluate python and does not require your user to know the python format.

 import sys list1 = sys.argv[1].split(',') list2 = [int(c) for c in sys.argv[2].split(',')] # if you want ints 
+5
source

You can also do the following:

let's say you have foo.py :

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

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

Output:

[1, 2, 3] <type 'list'>

+3
source

You need to run:

 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] 

Bash out

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 the second will give

 1 2 3 4 
+2
source

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

 import ast A = ast.literal_eval(strA) B = ast.literal_eval(strB) 
+1
source

Source: https://habr.com/ru/post/1232189/


All Articles