如何通过命令行将数组传递给python [复制]

时间:2022-12-14 00:06:24

This question already has an answer here:

这个问题在这里已有答案:

I am trying to pass an array to the python

我试图将数组传递给python

import sys
arr = sys.argv[1]
print(arr[2])

My command is

我的命令是

python3 test.py [1,2,3,4,5] 0

I hope the result it

我希望结果呢

2

However, it is

但是,确实如此

,

2 个解决方案

#1


11  

The elements of argv are strings, they're not parsed like literals in the program.

argv的元素是字符串,它们不像程序中的文字那样被解析。

You should just pass a comma-separated string (without the brackets):

您应该只传递逗号分隔的字符串(不带括号):

python3 test.py 1,2,3,4,5 0

and then use split() to convert it to an array.

然后使用split()将其转换为数组。

import sys
arr = sys.argv[1].split(',')
print(arr[2])

#2


5  

Commandline arguments are strings. Even integers have to be converted from string to int. If you use the list syntax you show in your example, you'll need to run the argument through some sort of parser (your own parser, something from ast, or eval-- but don't use eval). But there's a simpler way: Just write the arguments separately, and use a slice of sys.argv as your list. Space-separated arguments is the standard way to pass multiple arguments to a commandline program (e.g., multiple filenames to less, rm, etc.).

命令行参数是字符串。甚至整数也必须从string转换为int。如果你使用你在例子中显示的列表语法,你需要通过某种解析器(你自己的解析器,来自ast或eval--但不使用eval)来运行参数。但是有一种更简单的方法:只需单独编写参数,并使用一段sys.argv作为列表。以空格分隔的参数是将多个参数传递给命令行程序的标准方法(例如,将多个文件名传递给less,rm等)。

python3 test.py -n a b c 1 2 3

First you'd identify and skip arguments that have a different purpose (-n in the above example), then simply keep the rest:

首先,您要识别并跳过具有不同目的的参数(在上例中为-n),然后简单地保留其余参数:

arr = sys.argv[2:]
print(arr[3])   # Prints "1"

PS. You also need to protect any argument from the shell, by quoting any characters with special meaning(*, ;, spaces that are part of an argument, etc.). But that's a separate issue.

PS。您还需要通过引用具有特殊含义的任何字符(* ,;,作为参数的一部分的空格等)来保护shell中的任何参数。但这是一个单独的问题。

#1


11  

The elements of argv are strings, they're not parsed like literals in the program.

argv的元素是字符串,它们不像程序中的文字那样被解析。

You should just pass a comma-separated string (without the brackets):

您应该只传递逗号分隔的字符串(不带括号):

python3 test.py 1,2,3,4,5 0

and then use split() to convert it to an array.

然后使用split()将其转换为数组。

import sys
arr = sys.argv[1].split(',')
print(arr[2])

#2


5  

Commandline arguments are strings. Even integers have to be converted from string to int. If you use the list syntax you show in your example, you'll need to run the argument through some sort of parser (your own parser, something from ast, or eval-- but don't use eval). But there's a simpler way: Just write the arguments separately, and use a slice of sys.argv as your list. Space-separated arguments is the standard way to pass multiple arguments to a commandline program (e.g., multiple filenames to less, rm, etc.).

命令行参数是字符串。甚至整数也必须从string转换为int。如果你使用你在例子中显示的列表语法,你需要通过某种解析器(你自己的解析器,来自ast或eval--但不使用eval)来运行参数。但是有一种更简单的方法:只需单独编写参数,并使用一段sys.argv作为列表。以空格分隔的参数是将多个参数传递给命令行程序的标准方法(例如,将多个文件名传递给less,rm等)。

python3 test.py -n a b c 1 2 3

First you'd identify and skip arguments that have a different purpose (-n in the above example), then simply keep the rest:

首先,您要识别并跳过具有不同目的的参数(在上例中为-n),然后简单地保留其余参数:

arr = sys.argv[2:]
print(arr[3])   # Prints "1"

PS. You also need to protect any argument from the shell, by quoting any characters with special meaning(*, ;, spaces that are part of an argument, etc.). But that's a separate issue.

PS。您还需要通过引用具有特殊含义的任何字符(* ,;,作为参数的一部分的空格等)来保护shell中的任何参数。但这是一个单独的问题。