将列表从 YAML 文件传递到 python
Pass a list from YAML file to python
我正在使用谷歌应用引擎在 python 3.7 标准环境下创建一个网络应用程序。我正在将值从 app.yaml 文件传递到我的主脚本,但是,我似乎无法将列表从 yaml 文件传递到主文件。
这是我的 app.yaml 文件:
runtime: python37
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static
env_variables:
USERS: 'myemail@email.com'
USERS2: ['myemai@email.com', 'youremail@email.com']
USERS3:
- 'myemail@email.com'
- 'youremail@email.com'
USERS4:
- 'myemail@email.com'
- 'youremail@email.com'
这是我的 python 脚本:
import os
users = os.environ.get('USERS')
users2 = os.environ.get('USERS2')
users3 = os.environ.get('USERS3')
users4 = os.environ.get('USERS4')
变量用户 returns 'myemail@email.com'
正确。虽然,users2
、users3
和 users4
都是 return []
(一个空列表)。
环境变量是文本,因此仅使用 os.environ
获取它们是行不通的。
你声称你得到的是空列表,但我怀疑你实际上得到的是空字符串。在 Python 3.7 的 os.py
中没有任何东西可以做类似将字符串转换为(空)列表的事情。
实际上 app.yaml
似乎无法处理它获取的 YAML 中的序列,它可以做一些事情,比如用分隔符连接序列条目。在任何情况下 USERS3
和 USERS4
完全相同,只是缩进不同(USERS2 的顺序当然不同)
我建议您自己执行上述操作,同时在 YAML 中省略多余的引号:
runtime: python37
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static
env_variables:
USERS: myemail@email.com
USERS2: myemai@email.com:youremail@email.com
USERS3: myemail@email.com:youremail@email.com
USERS4: myemail@email.com:youremail@email.com
然后在你的Python中做
import os
def listified_env(k):
x = os.environ.get(k)
if ':' in x:
return x.split(':')
return x
users = listified_env('USERS')
users2 = listified_env('USERS2')
users3 = listified_env('USERS3')
users4 = listified_env('USERS4')
我正在使用谷歌应用引擎在 python 3.7 标准环境下创建一个网络应用程序。我正在将值从 app.yaml 文件传递到我的主脚本,但是,我似乎无法将列表从 yaml 文件传递到主文件。
这是我的 app.yaml 文件:
runtime: python37
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static
env_variables:
USERS: 'myemail@email.com'
USERS2: ['myemai@email.com', 'youremail@email.com']
USERS3:
- 'myemail@email.com'
- 'youremail@email.com'
USERS4:
- 'myemail@email.com'
- 'youremail@email.com'
这是我的 python 脚本:
import os
users = os.environ.get('USERS')
users2 = os.environ.get('USERS2')
users3 = os.environ.get('USERS3')
users4 = os.environ.get('USERS4')
变量用户 returns 'myemail@email.com'
正确。虽然,users2
、users3
和 users4
都是 return []
(一个空列表)。
环境变量是文本,因此仅使用 os.environ
获取它们是行不通的。
你声称你得到的是空列表,但我怀疑你实际上得到的是空字符串。在 Python 3.7 的 os.py
中没有任何东西可以做类似将字符串转换为(空)列表的事情。
实际上 app.yaml
似乎无法处理它获取的 YAML 中的序列,它可以做一些事情,比如用分隔符连接序列条目。在任何情况下 USERS3
和 USERS4
完全相同,只是缩进不同(USERS2 的顺序当然不同)
我建议您自己执行上述操作,同时在 YAML 中省略多余的引号:
runtime: python37
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static
env_variables:
USERS: myemail@email.com
USERS2: myemai@email.com:youremail@email.com
USERS3: myemail@email.com:youremail@email.com
USERS4: myemail@email.com:youremail@email.com
然后在你的Python中做
import os
def listified_env(k):
x = os.environ.get(k)
if ':' in x:
return x.split(':')
return x
users = listified_env('USERS')
users2 = listified_env('USERS2')
users3 = listified_env('USERS3')
users4 = listified_env('USERS4')