基本上我想迭代yaml文件,但它只打印yaml配置的最后一个值。
代码:
for application in config['applications']:
default_cname = '%s-%s.%s.elasticbeanstalk.com' % (application['cname'], application[‘name’], config['region'])
print (default_cname)
YAML文件:
sqa:
region: ap-northeast-1
applications:
- name: admin
cname: wp-kb-web
name: web
cname: wp-kb
预期产出:
wp-kb-web-admin.ap-northeast-1.elasticbeanstalk.com
wp-kb-web.ap-northeast-1.elasticbeanstalk.com
答案 0 :(得分:0)
您错过-
以在YAML中启动第二个应用程序定义。没有它,列表中只有一个项目,其中最新属性会覆盖第一个(重复)属性:
import yaml
yaml.load('''sqa:
region: ap-northeast-1
applications:
- name: admin
cname: wp-kb-web
name: web
cname: wp-kb'''
{'sqa':{'applications':[{'cname':'wp-kb','name':'web'}], 'region':'ap-northeast-1'}}
yaml.load('''sqa:
region: ap-northeast-1
applications:
- name: admin
cname: wp-kb-web
- name: web
cname: wp-kb'''
{'sqa':{'applications':[{'cname':'wp-kb-web','name':'admin'}, {'cname':'wp-kb','name':'web'}], 'region':'ap-northeast-1'}}