• <bdo id='HtTIV'></bdo><ul id='HtTIV'></ul>

  1. <tfoot id='HtTIV'></tfoot>

    <legend id='HtTIV'><style id='HtTIV'><dir id='HtTIV'><q id='HtTIV'></q></dir></style></legend>

      <small id='HtTIV'></small><noframes id='HtTIV'>

      <i id='HtTIV'><tr id='HtTIV'><dt id='HtTIV'><q id='HtTIV'><span id='HtTIV'><b id='HtTIV'><form id='HtTIV'><ins id='HtTIV'></ins><ul id='HtTIV'></ul><sub id='HtTIV'></sub></form><legend id='HtTIV'></legend><bdo id='HtTIV'><pre id='HtTIV'><center id='HtTIV'></center></pre></bdo></b><th id='HtTIV'></th></span></q></dt></tr></i><div id='HtTIV'><tfoot id='HtTIV'></tfoot><dl id='HtTIV'><fieldset id='HtTIV'></fieldset></dl></div>

      通过 ruamel.yaml 转储时如何在 yaml 文件中保留空值

      时间:2023-09-12
        <tbody id='GcgrH'></tbody>

        • <bdo id='GcgrH'></bdo><ul id='GcgrH'></ul>
          <i id='GcgrH'><tr id='GcgrH'><dt id='GcgrH'><q id='GcgrH'><span id='GcgrH'><b id='GcgrH'><form id='GcgrH'><ins id='GcgrH'></ins><ul id='GcgrH'></ul><sub id='GcgrH'></sub></form><legend id='GcgrH'></legend><bdo id='GcgrH'><pre id='GcgrH'><center id='GcgrH'></center></pre></bdo></b><th id='GcgrH'></th></span></q></dt></tr></i><div id='GcgrH'><tfoot id='GcgrH'></tfoot><dl id='GcgrH'><fieldset id='GcgrH'></fieldset></dl></div>
          <tfoot id='GcgrH'></tfoot>

          <small id='GcgrH'></small><noframes id='GcgrH'>

              • <legend id='GcgrH'><style id='GcgrH'><dir id='GcgrH'><q id='GcgrH'></q></dir></style></legend>
                本文介绍了通过 ruamel.yaml 转储时如何在 yaml 文件中保留空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有 YAML 文件 site.yaml:

                I have YAML file site.yaml:

                Kvm_BLOCK:
                  ip_address: 10.X.X.X
                  property: null
                  server_type: zone
                

                加载然后转储:

                ruamel.yaml.dump(site_yaml, new_file, Dumper=ruamel.yaml.RoundTripDumper)
                

                变成了

                Kvm_BLOCK:
                      ip_address: 10.X.X.X
                      property: 
                      server_type: zone
                

                如何在属性块中保留这个 null

                how to retain this null value in property block

                推荐答案

                YAML 1.2中的null值(构造为Python的None)可以表示为nullNullNULL~,指定为 这里.

                The null value in YAML 1.2 (constructed as Python's None) can be represented as null, Null, NULL and ~, as specified here.

                另外:

                内容为空的节点被解释为它们是具有空值的普通标量.此类节点通常被解析为空"值.

                Nodes with empty content are interpreted as if they were plain scalars with an empty value. Such nodes are commonly resolved to a "null" value.

                因此,您的 null 值并没有消失,它只是在使用 ruamel.yaml 时由 null 的默认表示不同地表示代码>RoundTripDump.如果再次加载该输出,您将再次获得 None 作为键 property

                Therefore your null value is not gone, it is just represented differently by the default representation for null in ruamel.yaml when using RoundTripDump. If you load that output again, you once more get a None as value for the key property

                如果您不喜欢这样,您可以通过以下方式更改 all None/null 值的输出:

                If that is not to your liking you can change the output for all None/null values by doing:

                import sys
                import ruamel.yaml
                
                
                yaml_str = """
                Kvm_BLOCK:
                  ip_address: 10.X.X.X
                  property: null
                  server_type: zone
                """
                
                
                def my_represent_none(self, data):
                    return self.represent_scalar(u'tag:yaml.org,2002:null', u'NULL')
                
                yaml = ruamel.yaml.YAML()
                yaml.representer.add_representer(type(None), my_represent_none)
                
                data = yaml.load(yaml_str)
                yaml.dump(data, sys.stdout)
                

                将转储:

                Kvm_BLOCK:
                  ip_address: 10.X.X.X
                  property: NULL
                  server_type: zone
                

                您可以通过在 Python 中创建不同的类(NULLNullnull 等)并使用不同的表示器来获得更细粒度的控制对于它们中的每一个(与 ruamel.yaml.scalarstring.py 中的 string 子类用于以不同方式表示字符串的方式非常相似(双引号,单引用,文字块样式标量).问题是你不能继承 NoneType 所以这不像字符串标量那样容易透明地完成.

                You can get finer grained control by creating different classes in Python (NULL, Null, null, etc. ) and have different representers for each of them (much in the same way that the string subclasses in ruamel.yaml.scalarstring.py are used to represent a string in different ways (double quoted, single quoted, literal block style scalar). The problem is that you cannot subclass NoneType so this is not so easily done transparently as with the string scalars.

                这篇关于通过 ruamel.yaml 转储时如何在 yaml 文件中保留空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何读取 YAML 文件中的组件,以便我可以使用 下一篇:在 app.yaml 中定义路由与在 AppEngine 中的 WSGIAppli

                相关文章

                最新文章

              • <small id='4E2xY'></small><noframes id='4E2xY'>

                <tfoot id='4E2xY'></tfoot>
                    <bdo id='4E2xY'></bdo><ul id='4E2xY'></ul>
                  <legend id='4E2xY'><style id='4E2xY'><dir id='4E2xY'><q id='4E2xY'></q></dir></style></legend>
                1. <i id='4E2xY'><tr id='4E2xY'><dt id='4E2xY'><q id='4E2xY'><span id='4E2xY'><b id='4E2xY'><form id='4E2xY'><ins id='4E2xY'></ins><ul id='4E2xY'></ul><sub id='4E2xY'></sub></form><legend id='4E2xY'></legend><bdo id='4E2xY'><pre id='4E2xY'><center id='4E2xY'></center></pre></bdo></b><th id='4E2xY'></th></span></q></dt></tr></i><div id='4E2xY'><tfoot id='4E2xY'></tfoot><dl id='4E2xY'><fieldset id='4E2xY'></fieldset></dl></div>