2017-10-20 21 views
0

Fondamentalement, je veux itérer sur le fichier yaml, mais il imprime seulement les dernières valeurs de la configuration de yaml.Python pour boucler sur yaml config

code:

for application in config['applications']: 

    default_cname = '%s-%s.%s.elasticbeanstalk.com' % (application['cname'], application[‘name’], config['region']) 

    print (default_cname) 

YAML fichier:

sqa: 
    region: ap-northeast-1 
    applications: 
    - name: admin 
     cname: wp-kb-web 
     name: web 
     cname: wp-kb 

Résultats escomptés:

wp-kb-web-admin.ap-northeast-1.elasticbeanstalk.com 
wp-kb-web.ap-northeast-1.elasticbeanstalk.com 

Répondre

0

Vous manquez un - pour commencer la deuxième définition d'application dans votre YAML. Sans elle, il y a un seul élément dans la liste où les derniers attributs écarte le premier (en double) les:

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 », 'nom': 'web'}], 'région': 'ap-nord-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', 'nom': 'admin'}, {'cname': 'wp-kb', 'nom': 'web'}}, 'région': 'ap-nord-est-1'}}

+0

Merci pour l'aide .. Cela me fait gagner beaucoup de temps .. – Raxso