[英]Python dynamic string representing argument[i]
I have a problem with Python and I tried searching for an answer here on SO but I can't seem to interpret any of the solutions supplied so far. 我在使用Python时遇到问题,尝试在SO上寻找答案,但似乎无法解释到目前为止提供的任何解决方案。 I supply my python program with command line arguments (sys.argv) and the number of these argument may change from one run to another so I decided that it would be best to make an for loop that creates a new variable for each argument.
我为我的python程序提供了命令行参数(sys.argv),这些参数的数量可能会从一次运行更改为另一次运行,因此我决定最好进行一个for循环,为每个参数创建一个新变量。 The first argument is as we all know the script name, I dont want to include this, the second argument is a password string and i dont want this either, so it starts from the third argument.
第一个参数是众所周知的脚本名称,我不想包括它,第二个参数是密码字符串,我也不想要这个,所以它从第三个参数开始。 The print at the end is just to see if it works or not.
最后的打印只是看它是否有效。
TempPersonNr = len(sys.argv)
TempPersonNr -= 2
VarPersonNr = ""
i = 1
for TempPersonNr in sys.argv[2:]:
VarPersonNr[i] = sys.argv[i]
print (VarPersonNr[i])
i += 1
Error: 错误:
Traceback (most recent call last):
File "HIDDEN", line 31, in <module>
VarPersonNr[i] = sys.argv[i]
TypeError: 'str' object does not support item assignment
I want the output to be something like this. 我希望输出是这样的。
Argument 1 = A
Argument 2 = B
Argument 3 = C
If i supply the program with the following command line arguments I want it to create 3 variables with the value as the argument. 如果我为程序提供以下命令行参数,则希望它创建3个变量,其值作为参数。 For example.
例如。
VarPersonNr1 (with the value of argument 1)
VarPersonNr2 (with the value of argument 2)
VarPersonNr3 (with the value of argument 3)
String objects are immutable you can't change it. 字符串对象是不可变的,无法更改。 So when you are trying to assign the i th variable of the string to sys.argv[i] it gives an error.
因此,当您尝试将字符串的第i个变量分配给sys.argv [i]时,会出现错误。
EDIT: As per your input and output you can directly assign the third value of sys.argv to your variable. 编辑:根据您的输入和输出,您可以直接将sys.argv的第三个值分配给您的变量。 varPersonNr3=sys.argv[i]
varPersonNr3 = sys.argv中[I]
If you want to add rest of the arguments to that variable use: VarPersonNr3.append(sys.argv[i]) 如果要将其余参数添加到该变量,请使用:VarPersonNr3.append(sys.argv [i])
EDIT: There are a couple of options for this: 1) You can use a dictionary to store the variable names as key and the sys.argv value as values: 编辑:为此,有几个选项:1)您可以使用字典将变量名存储为键,将sys.argv值存储为值:
d={}
TempPersonNr = len(sys.argv)
TempPersonNr -= 2
VarPersonNr = ""
i = 1
for TempPersonNr in sys.argv[2:]:
key="VarPersonNr"+str(i)
d[key]=sys.argv[i]
i += 1
This will give you a dictionary {"VarPersonNr3":'C',"VarPersonNr4":'D'} and so on 2) You can add all the values to a list and fetch it from there. 这将为您提供字典{“ VarPersonNr3”:'C',“ VarPersonNr4”:'D'},依此类推2)您可以将所有值添加到列表中并从那里获取。 3) This is a very bad option and should not be used .. but still:
3)这是一个非常糟糕的选择,不应使用..但仍然:
import sys
this = sys.modules[__name__]
i=1
for TempPersonNr in sys.argv[2:]:
setattr(this, 'VarPersonNr%s' % i, sys.argv[i])
i+=1
print(VarPersonNr3)
#this will give you the third value
Try using a dict or a list for this. 尝试为此使用字典或列表。
Try this, so you'll have a list (you can name new variables in the way you described). 尝试此操作,这样您将获得一个列表(可以按照描述的方式命名新变量)。 Also i think you don't get well how for loops work...
我也认为你对于for循环的工作方式不太满意...
TempPersonNr = len(sys.argv) - 2
VarPersonNr = []
for TempPerson in sys.argv[2:]:
VarPersonNr.append(TempPerson)
print (TempPerson)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.