<bdo id='vX9er'></bdo><ul id='vX9er'></ul>

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

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

      2. <small id='vX9er'></small><noframes id='vX9er'>

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

        python yaml.dump 缩进错误

        时间:2023-09-13

            <bdo id='1KgEi'></bdo><ul id='1KgEi'></ul>

                  <small id='1KgEi'></small><noframes id='1KgEi'>

                  <tfoot id='1KgEi'></tfoot>

                  <i id='1KgEi'><tr id='1KgEi'><dt id='1KgEi'><q id='1KgEi'><span id='1KgEi'><b id='1KgEi'><form id='1KgEi'><ins id='1KgEi'></ins><ul id='1KgEi'></ul><sub id='1KgEi'></sub></form><legend id='1KgEi'></legend><bdo id='1KgEi'><pre id='1KgEi'><center id='1KgEi'></center></pre></bdo></b><th id='1KgEi'></th></span></q></dt></tr></i><div id='1KgEi'><tfoot id='1KgEi'></tfoot><dl id='1KgEi'><fieldset id='1KgEi'></fieldset></dl></div>
                    <tbody id='1KgEi'></tbody>
                  <legend id='1KgEi'><style id='1KgEi'><dir id='1KgEi'><q id='1KgEi'></q></dir></style></legend>
                  本文介绍了python yaml.dump 缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在执行以下 python 代码:

                  I'm executing the following python code:

                  import yaml
                  
                  
                  foo = {
                      'name': 'foo',
                      'my_list': [{'foo': 'test', 'bar': 'test2'}, {'foo': 'test3', 'bar': 'test4'}],
                      'hello': 'world'
                  }
                  
                  print(yaml.dump(foo, default_flow_style=False))
                  

                  但正在打印:

                  hello: world
                  my_list:
                  - bar: test2
                    foo: test
                  - bar: test4
                    foo: test3
                  name: foo
                  

                  代替:

                  hello: world
                  my_list:
                    - bar: test2
                      foo: test
                    - bar: test4
                      foo: test3
                  name: foo
                  

                  如何以这种方式缩进 my_list 元素?

                  How can I indent the my_list elements this way?

                  推荐答案

                  这张票 表明当前的实现正确地遵循了规范:

                  This ticket suggests the current implementation correctly follows the spec:

                  -"、?"用于表示块集合条目的:"字符被人们认为是缩进的一部分.这由相关制作根据具体情况进行处理.

                  The "-", "?" and ":" characters used to denote block collection entries are perceived by people to be part of the indentation. This is handled on a case-by-case basis by the relevant productions.

                  在同一个线程上,还有 此代码片段(已修改以适合您的示例)以获得您正在寻找的行为:

                  On the same thread, there is also this code snippet (modified to fit your example) to get the behavior you are looking for:

                  import yaml
                  
                  class MyDumper(yaml.Dumper):
                  
                      def increase_indent(self, flow=False, indentless=False):
                          return super(MyDumper, self).increase_indent(flow, False)
                  
                  foo = {
                      'name': 'foo',
                      'my_list': [
                          {'foo': 'test', 'bar': 'test2'},
                          {'foo': 'test3', 'bar': 'test4'}],
                      'hello': 'world',
                  }
                  
                  print yaml.dump(foo, Dumper=MyDumper, default_flow_style=False)
                  

                  这篇关于python yaml.dump 缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 YAML 文件转换为 python dict 下一篇:ConfigObj/ConfigParser 与为 Python 设置文件使用 YAML

                  相关文章

                  最新文章

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

                  1. <small id='Iqu7j'></small><noframes id='Iqu7j'>

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