如何将列表作为环境变量传递? [英] How to pass a list as an environment variable?

查看:83
本文介绍了如何将列表作为环境变量传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将列表用作Python程序的一部分,并希望将其转换为环境变量.

I use a list as part of a Python program, and wanted to convert that to an environment variable.

所以,就像这样:

list1 = ['a.1','b.2','c.3']
for items in list1:
    alpha,number = items.split('.')
    print(alpha,number)

这给了我期望,

a 1
b 2
c 3

但是当我尝试将其设置为环境变量时,如下:

But when I try to set it as an environment variable, as:

export LIST_ITEMS = 'a.1', 'b.2', 'c.3'

然后做:

list1 = [os.environ.get("LIST_ITEMS")]
for items in list1:
    alpha,number = items.split('.')
    print(alpha,number)

我收到一个错误:ValueError: too many values to unpack

如何修改传递列表的方式或获取它的方式,以便获得与不使用env变量的输出相同的输出?

How do I modify the way I pass the list, or get it so that I have the same output as without using env variables?

推荐答案

我不确定为什么要通过环境变量来做到这一点,但是您可以这样做:

I'm not sure why you'd do it through the environment variables, but you can do this:

export LIST_ITEMS ="a.1 b.2 c.3"

在Python中:

list1 = [i.split(".") for i in os.environ.get("LIST_ITEMS").split(" ")] 

for k, v in list1:
    print(k, v)

这篇关于如何将列表作为环境变量传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆