<small id='4mxVv'></small><noframes id='4mxVv'>

  1. <tfoot id='4mxVv'></tfoot>
    • <bdo id='4mxVv'></bdo><ul id='4mxVv'></ul>

      <legend id='4mxVv'><style id='4mxVv'><dir id='4mxVv'><q id='4mxVv'></q></dir></style></legend>
    1. <i id='4mxVv'><tr id='4mxVv'><dt id='4mxVv'><q id='4mxVv'><span id='4mxVv'><b id='4mxVv'><form id='4mxVv'><ins id='4mxVv'></ins><ul id='4mxVv'></ul><sub id='4mxVv'></sub></form><legend id='4mxVv'></legend><bdo id='4mxVv'><pre id='4mxVv'><center id='4mxVv'></center></pre></bdo></b><th id='4mxVv'></th></span></q></dt></tr></i><div id='4mxVv'><tfoot id='4mxVv'></tfoot><dl id='4mxVv'><fieldset id='4mxVv'></fieldset></dl></div>
    2. 如何将 YAML 文件解析/读取到 Python 对象中?

      时间:2023-09-12

          <legend id='8qY3d'><style id='8qY3d'><dir id='8qY3d'><q id='8qY3d'></q></dir></style></legend>

          • <small id='8qY3d'></small><noframes id='8qY3d'>

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

                  <tbody id='8qY3d'></tbody>
              • 本文介绍了如何将 YAML 文件解析/读取到 Python 对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何将 YAML 文件解析/读取到 Python 对象中?

                How to parse/read a YAML file into a Python object?

                例如,这个 YAML:

                For example, this YAML:

                Person:
                  name: XYZ
                

                到这个 Python 类:

                To this Python class:

                class Person(yaml.YAMLObject):
                  yaml_tag = 'Person'
                
                  def __init__(self, name):
                    self.name = name
                

                顺便说一句,我正在使用 PyYAML.

                I am using PyYAML by the way.

                推荐答案

                如果你的 YAML 文件如下所示:

                If your YAML file looks like this:

                # tree format
                treeroot:
                    branch1:
                        name: Node 1
                        branch1-1:
                            name: Node 1-1
                    branch2:
                        name: Node 2
                        branch2-1:
                            name: Node 2-1
                

                你已经像这样安装了 PyYAML:

                And you've installed PyYAML like this:

                pip install PyYAML
                

                Python 代码如下所示:

                And the Python code looks like this:

                import yaml
                with open('tree.yaml') as f:
                    # use safe_load instead load
                    dataMap = yaml.safe_load(f)
                

                变量 dataMap 现在包含一个带有树数据的字典.如果你使用 PrettyPrint 打印 dataMap,你会得到类似的东西:

                The variable dataMap now contains a dictionary with the tree data. If you print dataMap using PrettyPrint, you will get something like:

                {
                    'treeroot': {
                        'branch1': {
                            'branch1-1': {
                                'name': 'Node 1-1'
                            },
                            'name': 'Node 1'
                        },
                        'branch2': {
                            'branch2-1': {
                                'name': 'Node 2-1'
                            },
                            'name': 'Node 2'
                        }
                    }
                }
                

                所以,现在我们已经了解了如何将数据导入 Python 程序.保存数据同样简单:

                So, now we have seen how to get data into our Python program. Saving data is just as easy:

                with open('newtree.yaml', "w") as f:
                    yaml.dump(dataMap, f)
                

                你有一本字典,现在你必须把它转换成一个 Python 对象:

                You have a dictionary, and now you have to convert it to a Python object:

                class Struct:
                    def __init__(self, **entries): 
                        self.__dict__.update(entries)
                

                那么你可以使用:

                >>> args = your YAML dictionary
                >>> s = Struct(**args)
                >>> s
                <__main__.Struct instance at 0x01D6A738>
                >>> s...
                

                并遵循将 Python dict 转换为对象".

                有关更多信息,您可以查看 pyyaml.org 和 这个.

                For more information you can look at pyyaml.org and this.

                这篇关于如何将 YAML 文件解析/读取到 Python 对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Python 中使用设置文件的最佳做法是什么? 下一篇:Python 中是否有任何支持将长字符串转储为块文字

                相关文章

                最新文章

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

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

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