【发布时间】:2018-04-01 06:02:40
【问题描述】:
我提供了下面的代码。我只是想知道是否有更好、更简洁的方法将整个索引加载到变量中,而不是手动指定每个...
Python 代码
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, 'config.yaml')
with open(file_path, 'r') as stream:
index = 'two'
load = yaml.load(stream)
USER = load[index]['USER']
PASS = load[index]['PASS']
HOST = load[index]['HOST']
PORT = load[index]['PORT']
...
YAML 配置
one:
USER: "john"
PASS: "qwerty"
HOST: "127.0.0.1"
PORT: "20"
two:
USER: "jane"
PASS: "qwerty"
HOST: "196.162.0.1"
PORT: "80"
【问题讨论】:
-
这提供了我当前如何设置代码的示例。换句话说,我希望程序自动导入所有变量。
标签: python python-3.x yaml pyyaml