• <small id='L5KdD'></small><noframes id='L5KdD'>

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

      1. 如何从 ruamel.yaml 转储的输出中删除 2 个空格?

        时间:2023-09-13

          <tbody id='LXapo'></tbody>

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

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

                • <bdo id='LXapo'></bdo><ul id='LXapo'></ul>
                • 本文介绍了如何从 ruamel.yaml 转储的输出中删除 2 个空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  yaml.indent(sequence=4, offset=2) 的帮助下,输出是正确的,但每一行都有额外的空间,我知道这是由于上述缩进功能.有什么方法可以从每行中删除 2 个额外的空格(我不会使用 strip()).

                  With the help of yaml.indent(sequence=4, offset=2) the output is correct but there is extra space coming in each line and I know it is due to above indent function. Is there any way to remove the 2 extra spaces from each line(I don't wont to use strip()).

                  代码:

                  import sys
                  import ruamel.yaml
                  
                  data = [{'item': 'Food_eat', 'Food': {'foodNo': 42536216,'type': 'fruit','moreInfo': ['organic']}}]
                  
                  yaml = ruamel.yaml.YAML()
                  yaml.indent(sequence=4, offset=2)
                  yaml.dump(data, sys.stdout)
                  

                  以上代码的输出:

                    - item: Food_eat
                      Food:
                        foodNo: 42536216
                        type: fruit
                        moreInfo:
                          - organic
                  

                  需要的输出:

                  - item: Food_eat
                    Food:
                      foodNo: 42536216
                      type: fruit
                      moreInfo:
                        - organic
                  

                  PS:我从这个 stackoverflow 问题中得到了帮助:如何将字典和列表安全转储到 YAML 中?

                  P.S: I have taken help from this stackoverflow question by me: How to safe_dump the dictionary and list into YAML?

                  推荐答案

                  与其说是缩进,不如说是序列的偏移项指标.此偏移量取自项目之前的空间如果根节点是一个列表,这会给出正确的 YAML,但它看起来次优.

                  It is not so much the indent, as well as the offset of the sequence item indicator. This offset is taken within the space before the item and if the root node is a list, this gives correct YAML, but it looks sub-optimal.

                  我一直在寻找解决这个问题,但没有想出一个好的解决方案.直到我您是否必须对输出进行后处理,这很容易完成:

                  I have been looking at fixing this, but have not come up with a good solution. Until I do you'll have to post-process your output, which can be easily done:

                  import sys
                  import ruamel.yaml
                  
                  data = [{'item': 'Food_eat', 'Food': {'foodNo': 42536216,'type': 'fruit','moreInfo': ['organic']}}]
                  
                  def strip_leading_double_space(stream):
                      if stream.startswith("  "):
                          stream = stream[2:]
                      return stream.replace("
                    ", "
                  ")
                      # you could also do that on a line by line basis
                      # return "".join([s[2:] if s.startswith("  ") else s for s in stream.splitlines(True)])
                  
                  
                  yaml = ruamel.yaml.YAML()
                  yaml.indent(sequence=4, offset=2)
                  print('# < to show alignment')
                  yaml.dump(data, sys.stdout, transform=strip_leading_double_space)
                  

                  给出:

                  # < to show alignment
                  - item: Food_eat
                    Food:
                      foodNo: 42536216
                      type: fruit
                      moreInfo:
                        - organic
                  

                  当然,如果额外的行首空格会更有效一开始就不会生成.

                  Of course it would be more efficient if the extra start-of-line spaces would not be generated in the first place.

                  这篇关于如何从 ruamel.yaml 转储的输出中删除 2 个空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 yamltodb 将 YAML 数据转换为数据框 下一篇:GAE python中的YAML文件url和脚本

                  相关文章

                  最新文章

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

                    <i id='ya6jv'><tr id='ya6jv'><dt id='ya6jv'><q id='ya6jv'><span id='ya6jv'><b id='ya6jv'><form id='ya6jv'><ins id='ya6jv'></ins><ul id='ya6jv'></ul><sub id='ya6jv'></sub></form><legend id='ya6jv'></legend><bdo id='ya6jv'><pre id='ya6jv'><center id='ya6jv'></center></pre></bdo></b><th id='ya6jv'></th></span></q></dt></tr></i><div id='ya6jv'><tfoot id='ya6jv'></tfoot><dl id='ya6jv'><fieldset id='ya6jv'></fieldset></dl></div>
                  1. <tfoot id='ya6jv'></tfoot>
                  2. <small id='ya6jv'></small><noframes id='ya6jv'>

                    1. <legend id='ya6jv'><style id='ya6jv'><dir id='ya6jv'><q id='ya6jv'></q></dir></style></legend>