• <tfoot id='eCFnl'></tfoot>

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

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

      1. <legend id='eCFnl'><style id='eCFnl'><dir id='eCFnl'><q id='eCFnl'></q></dir></style></legend>
      2. 用 Python 编辑 YAML 文件

        时间:2023-09-12
              <bdo id='2BjgW'></bdo><ul id='2BjgW'></ul>

              <small id='2BjgW'></small><noframes id='2BjgW'>

                <legend id='2BjgW'><style id='2BjgW'><dir id='2BjgW'><q id='2BjgW'></q></dir></style></legend>
                  <tbody id='2BjgW'></tbody>
                <tfoot id='2BjgW'></tfoot>

                  <i id='2BjgW'><tr id='2BjgW'><dt id='2BjgW'><q id='2BjgW'><span id='2BjgW'><b id='2BjgW'><form id='2BjgW'><ins id='2BjgW'></ins><ul id='2BjgW'></ul><sub id='2BjgW'></sub></form><legend id='2BjgW'></legend><bdo id='2BjgW'><pre id='2BjgW'><center id='2BjgW'></center></pre></bdo></b><th id='2BjgW'></th></span></q></dt></tr></i><div id='2BjgW'><tfoot id='2BjgW'></tfoot><dl id='2BjgW'><fieldset id='2BjgW'></fieldset></dl></div>
                1. 本文介绍了用 Python 编辑 YAML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个如下所示的 YAML 文件:

                  I have a YAML file that looks like this:

                  # Sense 1
                  - name  : sense1
                    type  : float
                    value : 31
                  
                  # sense 2
                  - name  : sense2
                    type  : uint32_t
                    value : 1488
                  
                  # Sense 3
                  - name  : sense3
                    type  : int32_t
                    value : 0
                  
                  - name  : sense4
                    type  : int32_t
                    value : 0
                  - name  : sense5
                    type  : int32_t
                    value : 0
                  - name  : sense6
                    type  : int32_t
                    value : 0
                  

                  我想使用 Python 打开这个文件,更改一些值(见上文)并关闭文件.我该怎么做?

                  I want to use Python to open this file, change some of the values (see above) and close the file. How can I do that ?

                  例如我想设置 sense2[value]=1234,保持 YAML 输出不变.

                  For instance I want to set sense2[value]=1234, keeping the YAML output the same.

                  推荐答案

                  如果您关心保留映射键的顺序、注释和根级序列元素之间的空格,例如因为此文件受修订控制,所以您应该使用 ruamel.yaml (免责声明:我是该软件包的作者).

                  If you care about preserving the order of your mapping keys, the comment and the white space between the elements of the root-level sequence, e.g. because this file is under revision control, then you should use ruamel.yaml (disclaimer: I am the author of that package).

                  假设您的 YAML 文档在文件 input.yaml 中:

                  Assuming your YAML document is in the file input.yaml:

                  import sys
                  import ruamel.yaml
                  
                  yaml = ruamel.yaml.YAML()
                  # yaml.preserve_quotes = True
                  with open('input.yaml') as fp:
                      data = yaml.load(fp)
                  for elem in data:
                      if elem['name'] == 'sense2':
                           elem['value'] = 1234
                           break  # no need to iterate further
                  yaml.dump(data, sys.stdout)
                  

                  给予:

                  # Sense 1
                  - name: sense1
                    type: float
                    value: 31
                  
                  # sense 2
                  - name: sense2
                    type: uint32_t
                    value: 1234
                  
                  # Sense 3
                  - name: sense3
                    type: int32_t
                    value: 0
                  
                  - name: sense4
                    type: int32_t
                    value: 0
                  - name: sense5
                    type: int32_t
                    value: 0
                  - name: sense6
                    type: int32_t
                    value: 0
                  

                  这篇关于用 Python 编辑 YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 中是否有任何支持将长字符串转储为块文字 下一篇:解析 YAML,返回行号

                  相关文章

                  最新文章

                    <tfoot id='0yDae'></tfoot>

                    <legend id='0yDae'><style id='0yDae'><dir id='0yDae'><q id='0yDae'></q></dir></style></legend>
                  1. <small id='0yDae'></small><noframes id='0yDae'>

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