【问题标题】:How can I split YAML into different files in python?如何在 python 中将 YAML 拆分为不同的文件?
【发布时间】:2021-01-19 19:50:30
【问题描述】:

我使用 yaml 库中的 safe_load 在 python 中的变量中有这个 yaml_file:

domainInfo:
    AdminUserName: '--FIX ME--'
    AdminPassword: '--FIX ME--'
topology:
    Name: 'wld-pil-10'
    ConfigBackupEnabled: true
    AdminServerName: 'wls-pil-10-sa-adm-n0'
    DomainVersion: 12.2.1.4.0
    ProductionModeEnabled: true
    ArchiveConfigurationCount: 20
    Cluster:
        'test-bruno-jee-r01a-c01':
            ClientCertProxyEnabled: true
            WeblogicPluginEnabled: true
    Server:
        'wls-pil-10-sa-adm-n0':
            ListenPort: 11030
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            Machine: 'wlm-pil-10-n0'
        'test-bruno-jee-r01a-it-c01-m1-n1':
            ListenPort: 10022
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            NMSocketCreateTimeoutInMillis: 30000
            Machine: 'wlm-pil-10-n1'
        'test-bruno-jee-r02a-it-c01-m1-n1':
            ListenPort: 10025
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            NMSocketCreateTimeoutInMillis: 30000
            Machine: 'wlm-pil-10-n2'

为了拆分此 yaml 文件,我尝试将键和值放入新字典中,但没有成功。我错过了什么?我知道我需要某种方式的字典,是否需要使用其他模块,例如 pyyaml 或 ruamel?

yaml_cluster = {}
yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]
yaml_cluster["topology"]["AdminServerName"] = yaml_file["topology"]["AdminServerName"]

结果:

致命:[wls-pil-103-sa-adm-n0]:失败! => {“改变”:真,“味精”: "非零返回码", "rc": 1, "stderr": "Traceback (最近 最后调用):\n 文件 "/tmp/ansible-tmp-1611083722.9917288-55849-215378473850896/split_yaml.py", 第 32 行,在 \n yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]\nKeyError: 'topology'\n", "stderr_lines": ["Traceback (最近一次调用最后一次):", " File "/tmp/ansible-tmp-1611083722.9917288-55849-215378473850896/split_yaml.py", 第 32 行,在 ", " yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]", "KeyError: 'topology'"], "stdout": "", "stdout_lines": []}

【问题讨论】:

    标签: python dictionary yaml


    【解决方案1】:

    您遇到的错误是因为 yaml_cluster["topology"]["Name"] 被分配了一个值,但其中任何一个键都不存在。 yaml_cluster["topology"] 不存在,因此您不能为键 Name 分配任何内容。

    标准库中的Collections 模块提供了一个类collections.defaultdict,如果键不存在,它会为其提供默认值。在您的情况下,默认值为空字典的defaultdictyaml_cluster["topology] 的值设置为{}(空字典),然后可以将键Name 分配给字典的值。

    from collections import defaultdict
    
    yaml_cluster = defaultdict(dict) # Specifying dictionary as default value for missing keys
    yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]
    yaml_cluster["topology"]["AdminServerName"] = yaml_file["topology"]["AdminServerName"]
    

    【讨论】:

    • 但现在当我将变量保存在文件中时会得到类似的结果:!!python/object/apply:collections.defaultdict args: - !!python/name:builtins.dict '' dictitems: topology: AdminServerName: wls-pil-103-sa-adm-n0 Name: wld-pil-103
    • @shamaN 一系列原因可能导致该错误含糊不清。我建议使用尽可能多的示例代码和一些日志文件启动一个新的 stackoverflow 线程。为了帮助其他人尽快回答您的问题,请关注guide on writing questions
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2021-02-14
    相关资源
    最近更新 更多