How to pass a list as an environment variable?

后端 未结 4 2081
猫巷女王i
猫巷女王i 2021-02-07 01:52

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

So, it\'s like this:

list1 = [\'a.1\',\'b.2\',\'c.3\']
         


        
4条回答
  •  一整个雨季
    2021-02-07 02:43

    If you want to set the environment variable using that format, this would work:

    from ast import literal_eval
    
    list1 = [literal_eval(e.strip()) for e in os.environ["LIST_ITEMS"].split(',')]
    for item in list1:
        alpha,number = item.split('.')
        print alpha, number
    

    Output:

    a 1
    b 2
    c 3
    

提交回复
热议问题