Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据环境变量的请求创建尽可能多的环境变量_Python_Python 3.x_Environment Variables - Fatal编程技术网

Python 根据环境变量的请求创建尽可能多的环境变量

Python 根据环境变量的请求创建尽可能多的环境变量,python,python-3.x,environment-variables,Python,Python 3.x,Environment Variables,所以我想创建一个可变数量的环境变量,其中servnuber是我想要的环境变量数量,它本身作为环境变量提供 servnumber=os.environ['SERV_NUMBER'] vartest='host' envtest='MY_HOST' for x in range(1, servnumber): vartest+str(x)=os.environ[envtest+str(x)] 我期望的结果是,如果servnumber为3,则创建 host1=os.environ['MY_H

所以我想创建一个可变数量的环境变量,其中
servnuber
是我想要的环境变量数量,它本身作为环境变量提供

servnumber=os.environ['SERV_NUMBER']
vartest='host'
envtest='MY_HOST'
for x in range(1, servnumber):
    vartest+str(x)=os.environ[envtest+str(x)]
我期望的结果是,如果servnumber为3,则创建

host1=os.environ['MY_HOST1']
host2=os.environ['MY_HOST2']
host3=os.environ['MY_HOST3']
但我得到了:

File "variableservers.py", line 49
    vartest+str(x)=os.environ[envtest+str(x)]
    ^ 
SyntaxError: can't assign to operator

这回答了你的问题吗?TL;博士,别那么做。。。使用列表代替…你的意思是将变量保存在列表中,而不是host1、host2等?我不能写评论,因为我应该更改所有代码,但它可能会工作,让我试着回答你的问题吗?TL;博士,别那么做。。。改为使用列表…你是说将变量保存在列表中而不是host1、host2等?我不能简单地写注释,因为我应该更改所有代码,但它可能会工作,让我试试