I am using getopt to process a command line optional argument, which should accept a list. Something like this:
foo.py --my_开发者_JS百科list=[1, 2, 3, 4,5]
But this trims everything after "[1,"
My questions are: A) Is there a way to specify a list without converting it into a string? (using getopt)
B) If I am to convert the list into a string, how to convert this list to a string? e.g. something like mylist.split("?") to get rid of square brackets ?? is there a better way?
Thank you
There are two options that I can think of:
- Use optparse, and use
append
action to specify what you want to do as:foo.py --my_list=1 --my_list=2 ...
. - Specify your commandline as
foo.py --my_list='1,2,3,4,5'
, and then usex.split(',')
to get your values in a list. You can usegetopt
oroptparse
for this method.
The advantage of the first method is that you can get integer values in the list directly, at the expense of the commandline being longer (but you can add a single-charecter option for --my_list
if you want). The advantage of the second is shorter command line, but after the split()
, you need to convert the string values '1'
, '2'
, etc., to integers (pretty easy as well).
If I can't use a standard parser (optparse or argparse) to my application then I use the ast.literal_eval function to parse input arguments of type list as follows:
import sys, ast
inputList = ast.literal_eval( sys.argv[1] )
print type( inputList )
print inputList
Let suppose that this code is stored in testParser.py file. By executing the script:
$ python testParser.py "[1,2,3,4, [123, 456, 789], 'asdasd']"
we get the following output:
<type 'list'>
[1, 2, 3, 4, [123, 456, 789], 'asdasd']
So, using the secure enough ast.literal_eval function and inserting the list as a string of code we have the desirable result.
Useful links:
Using python's eval() vs. ast.literal_eval()?
http://docs.python.org/2/library/functions.html?highlight=eval#eval
Maybe you should just enclose the argument in quotes?
foo.py "--my_list=[1, 2, 3, 4,5]"
Otherwise every space will be treated as a separator for arguments.
An updated way is to use the argparse
library and add the list as a command line argument. This way, you don't have to do any manual parsing.
Example:
parser = argparse.ArgumentParser()
parser.add_argument(
"-values",
nargs="*", # expects ≥ 0 arguments
type=int,
default=[35, 40, 50, 60, 70, 80, 90], # default list if no arg value
)
Which you would then call like this:
python3 someprogram.py -values 1 2 3
From the python optparse help page: "
parser.add_option("-f")
parser.add_option("-p", type="float", nargs=3, dest="point")
As it parses the command line
-f foo.txt -p 1 -3.5 4 -fbar.txt
optparse will set
options.f = "foo.txt"
options.point = (1.0, -3.5, 4.0)
options.f = "bar.txt"
"
I have been redirected from the dup question:
How to pass an array to python through command line - Stack Overflow
I’m answering that question:
The arguments on the command line are strings, they're not parsed like literals in the program.
argv construct the strings automatically to a list from command line arguments (as separated by spaces), in short,
sys.argv
is a list.
from sys import argv
cmd = "arr = {sys.argv[1]}".format(argv)
exec(cmd)
print(arr[2])
精彩评论