<bdo id='lW0Il'></bdo><ul id='lW0Il'></ul>
  1. <tfoot id='lW0Il'></tfoot>

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

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

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

      将 YAML 文件转换为 python dict

      时间:2023-09-13
          <tbody id='CimzH'></tbody>

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

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

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

              • 本文介绍了将 YAML 文件转换为 python dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在将 YAML 文件中的文档映射到 dict 并正确映射它们时遇到以下问题.

                I am having the following problem of mapping documents within a YAML file to a dict and properly mapping them.

                我有以下 YAML 文件,它代表一个服务器 (db.yml):

                I have the following YAML file, which represents a server (db.yml):

                instanceId: i-aaaaaaaa
                     environment:us-east
                     serverId:someServer
                     awsHostname:ip-someip
                     serverName:somewebsite.com
                     ipAddr:192.168.0.1
                     roles:[webserver,php]
                

                我加载了这个 YAML 文件,我可以毫无问题地这样做,我想我明白了.

                I load this YAML file, which I can do without any problems, I think I understand that.

                instanceId = getInstanceId()
                stream = file('db.yml', 'r')
                dict = yaml.load_all(stream)
                
                for key in dict:
                    if key in dict == "instanceId":
                        print key, dict[key]
                

                我希望逻辑如下所示:

                • 加载yaml,映射到dict
                • 查看文档中的每个 dict,如果 instanceIdgetInstanceId() 设置的匹配,则打印出该文档的所有键和值.
                • load yaml, map to dict
                • look in every dict in the document, if the instanceId matches that which was set by getInstanceId(), then print out all of the keys and values for that document.

                如果我从命令行查看地图数据结构,我会得到:

                If I look at the map data structure from the command line, I get:

                {'instanceId': 'i-aaaaaaaa environment:us-east serverId:someServer awsHostname:ip-someip serverName:someserver ipAddr:192.168.0.1 roles:[webserver,php]'}
                

                我想我可能不正确地为 YAML 文件创建数据结构,并且在匹配 dict 上的内容时,我有点迷失了.

                I think I might be creating the data structure for the YAML file improperly, and on matching the contents on the dict, I am a bit lost.

                旁注:我无法使用 yaml.load() 加载此文件中的所有文档,我尝试了 yaml.load_all(),这似乎可行,但我的主要问题仍然存在.

                Side note: I cannot load all of the documents in this file using yaml.load(), I tried yaml.load_all(), which seems to work but my main issue still exists.

                推荐答案

                我认为你的 yaml 文件应该看起来像(或者至少是类似的,所以它的结构是正确的):

                I think your yaml file should look like (or at least something like, so it's structured correctly anyway):

                instance:
                     Id: i-aaaaaaaa
                     environment: us-east
                     serverId: someServer
                     awsHostname: ip-someip
                     serverName: somewebsite.com
                     ipAddr: 192.168.0.1
                     roles: [webserver,php]
                

                然后,yaml.load(...) 返回:

                {'instance': {'environment': 'us-east', 'roles': ['webserver', 'php'], 'awsHostname': 'ip-someip', 'serverName': 'somewebsite.com', 'ipAddr': '192.168.0.1', 'serverId': 'someServer', 'Id': 'i-aaaaaaaa'}}
                

                你可以从那里去......

                And you can go from there...

                所以像这样使用:

                >>> for key, value in yaml.load(open('test.txt'))['instance'].iteritems():
                    print key, value
                
                
                environment us-east
                roles ['webserver', 'php']
                awsHostname ip-someip
                serverName somewebsite.com
                ipAddr 192.168.0.1
                serverId someServer
                Id i-aaaaaaaa
                

                这篇关于将 YAML 文件转换为 python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python中的Yaml合并 下一篇:python yaml.dump 缩进错误

                相关文章

                最新文章

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

              • <tfoot id='XH49K'></tfoot>
                  <bdo id='XH49K'></bdo><ul id='XH49K'></ul>

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

              • <legend id='XH49K'><style id='XH49K'><dir id='XH49K'><q id='XH49K'></q></dir></style></legend>