Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在yaml文件中填充默认参数_Python_Yaml - Fatal编程技术网

如何使用python在yaml文件中填充默认参数

如何使用python在yaml文件中填充默认参数,python,yaml,Python,Yaml,我有一个包含以下内容的YAML文件: image_name: ubuntu components: image_name: ubuntu image_flavors: [medium, large] image_ip : 192.168.12.134 imageuser : ubuntu image_

我有一个包含以下内容的YAML文件:

       image_name: ubuntu
       components:
                 image_name: ubuntu
                 image_flavors: [medium, large]
                 image_ip : 192.168.12.134
                 imageuser : ubuntu
                 image_pass : ubuntu
                 image_config: /home/ubuntu/
                 image_net_type: vlan 
                 image_switch_type: ovs
我已经实现了一个脚本,用于加载YAML文件和dict格式的o/p

       with open("test.yaml", "r") as input:
        try:
            a = yaml.safe_load(input)
            print "Parsing YAML file is completed"
            print a
        except yaml.YAMLError as error:
            print(error)
我的dict格式如下:

{'image_name': 'ubuntu', 'components': {'image_ip': '192.168.12.134', 
 'image_pass': 'ubuntu', 'image_switch_type': 'ovs', 'imageuser': 'ubuntu',
 'image_name': 'ubuntu', 'image_flavors': ['medium', 'large'], 
 'image_net_type': 'vlan', 'image_config': '/home/ubuntu/'}}

如果任何键没有值,如何填充默认参数?

一个选项是,当您从yaml解析的dict中获取键的值时,使用dict的
get()
方法。因此

value = a.get('key', 'default')

如果键
key
位于
a
中,它将返回值,否则它将返回您指定为默认值的任何值。在上面的例子中,这将是
'default'

嗯,你的意思是这样的:

default_values = {
    'image_name': 'some',
    'components': {
         'image_ip': '192.168.1.1',
         'image_pass': 'default_password',
         'image_switch_type': 'ovs',
         'imageuser': 'default_user',
         'image_name': 'default_name',
         'image_flavors': ['medium', 'large'],
         'image_net_type': 'vlan',
         'image_config': '/default/home/'
     }
}

def setdefault_recursively(tgt, default = default_values):
    for k in default:
        if isinstance(default[k], dict): # if the current item is a dict,
            # expand it recursively
            setdefault_recursively(tgt.setdefault(k, {}), default[k])
        else:
            # ... otherwise simply set a default value if it's not set before
            tgt.setdefault(k, default[k])

dic = { 'image_name': 'ubuntu', 'components': { 'image_name': 'ubuntu' }

setdefault_recursively(dic)
?

这将用默认值递归地填充从yaml文件读取的
dic

{'components': {'image_config': '/default/home/',
            'image_flavors': ['medium', 'large'],
            'image_ip': '192.168.1.1',
            'image_name': 'ubuntu',
            'image_net_type': 'vlan',
            'image_pass': 'default_password',
            'image_switch_type': 'ovs',
            'imageuser': 'default_user'},
 'image_name': 'ubuntu'}

也就是说,结构中所有未从YAML文件显式设置的设置都已从
default\u values
,uhmm,递归地接收其默认值。

hello,上面的func很有用,但看起来有些复杂。你能详细说明一下吗?我无法理解你的逻辑。你的YAML文件有映射键,你的Python字典也有键。请编辑您的帖子,以明确您希望将其中哪一个用作另一个的默认参数。