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

      <i id='C1l7K'><tr id='C1l7K'><dt id='C1l7K'><q id='C1l7K'><span id='C1l7K'><b id='C1l7K'><form id='C1l7K'><ins id='C1l7K'></ins><ul id='C1l7K'></ul><sub id='C1l7K'></sub></form><legend id='C1l7K'></legend><bdo id='C1l7K'><pre id='C1l7K'><center id='C1l7K'></center></pre></bdo></b><th id='C1l7K'></th></span></q></dt></tr></i><div id='C1l7K'><tfoot id='C1l7K'></tfoot><dl id='C1l7K'><fieldset id='C1l7K'></fieldset></dl></div>
      • <bdo id='C1l7K'></bdo><ul id='C1l7K'></ul>
      <tfoot id='C1l7K'></tfoot>
    2. 使用 Python 读取 YAML 文件会导致 yaml.composer.Compo

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

            <tfoot id='FbbDw'></tfoot>

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

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

                <bdo id='FbbDw'></bdo><ul id='FbbDw'></ul>
              • 本文介绍了使用 Python 读取 YAML 文件会导致 yaml.composer.ComposerError: expected a single document in the stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个看起来像这样的 yaml 文件

                I have a yaml file that looks like

                ---
                level_1: "test"
                level_2: 'NetApp, SOFS, ZFS Creation'
                request: 341570
                ---
                level_1: "test"
                level_2: 'NetApp, SOFS, ZFS Creation'
                request: 341569
                ---
                level_1: "test"
                level_2: 'NetApp, SOFS, ZFS Creation'
                request: 341568
                

                我能够在使用 YAML 的 Perl 中正确读取此内容,但在使用 YAML 的 python 中无法正确读取.它失败并出现错误:

                I am able to read this correctly in Perl using YAML but not in python using YAML. It fails with the error:

                预期流中有单个文档

                程序:

                import yaml
                
                stram = open("test", "r")
                print yaml.load(stram)
                

                错误:

                Traceback (most recent call last):
                  File "abcd", line 4, in <module>
                    print yaml.load(stram)
                  File "/usr/local/pkgs/python-2.6.5/lib/python2.6/site-packages/yaml/__init__.py", line 58, in load
                    return loader.get_single_data()
                  File "/usr/local/pkgs/python-2.6.5/lib/python2.6/site-packages/yaml/constructor.py", line 42, in get_single_data
                    node = self.get_single_node()
                  File "/usr/local/pkgs/python-2.6.5/lib/python2.6/site-packages/yaml/composer.py", line 43, in get_single_node
                    event.start_mark)
                yaml.composer.ComposerError: expected a single document in the stream
                  in "test", line 2, column 1
                but found another document
                  in "test", line 5, column 1
                

                推荐答案

                yaml 文档用 --- 分隔,如果任何流(例如文件)包含多个文档,那么你应该使用 yaml.load_all 函数而不是 yaml.load.代码:

                The yaml documents are separated by ---, and if any stream (e.g. a file) contains more than one document then you should use the yaml.load_all function rather than yaml.load. The code:

                import yaml
                
                stream = open("test", "r")
                docs = yaml.load_all(stream)
                for doc in docs:
                    for k,v in doc.items():
                        print k, "->", v
                    print "
                ",
                

                问题中提供的输入文件的结果:

                results in for the input file as provided in the question:

                request -> 341570
                level_1 -> test
                level_2 -> NetApp, SOFS, ZFS Creation
                
                request -> 341569
                level_1 -> test
                level_2 -> NetApp, SOFS, ZFS Creation
                
                request -> 341568
                level_1 -> test
                level_2 -> NetApp, SOFS, ZFS Creation
                

                这篇关于使用 Python 读取 YAML 文件会导致 yaml.composer.ComposerError: expected a single document in the stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将 YAML 文件转换为 Python JSON 对象 下一篇:在 python 中读取 YAML 配置文件并使用变量

                相关文章

                最新文章

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

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

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

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