2017-02-27 183 views
0

我想從蟒蛇值,把它們放在YAML:如何讓Python更新YAML的變量?

import ruamel.yaml 

file_name = 'input.yml' 
from ruamel.yaml.util import load_yaml_guess_indent 

config, ind, bsi = load_yaml_guess_indent(open(file_name)) 

instances = config['instances'] 
print instances 
instances['hostname'] = raw_input('What is the host name>>') 
instances['syslog'] = raw_input ('what is the Syslog ip address>>') 
instances['prefix'] = raw_input('What is the first prefix>>') 
instances['prefix'] = raw_input ('what is the second address>>') 

input.yml

--- 
instances: 
     hostname: <name>    # update with IP 
     syslog: <ip> # update with user name 
     interfaces: 
     - name: GigabitEthernet0/0 
      prefix: <first prefix>   
     - name: GigabitEthernet0/1 
      prefix: <second prefix> 

主機和系統日誌部分工作,但我不能讓前綴工作。

回答

1

此:

instances['prefix'] = raw_input('What is the first prefix>>') 
instances['prefix'] = raw_input ('what is the second address>>') 

不工作(如果它願意,你會更新兩次同樣的事情)。你需要考慮到prefix是序列(Python的list)的元素上的鍵是該鍵interfaces值:

instances['interfaces'][0]['prefix'] = raw_input('What is the first prefix>>') 
instances['interfaces'][1]['prefix'] = raw_input ('what is the second address>>') 
+0

是的,它的工作完美。非常感謝。 – NANIS

+0

@NANIS下一次,如果遇到問題,請提供您正在獲取的錯誤文本。特別是當你不能提供完整的程序,這將不可能爲其他人產生錯誤。 – Anthon