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

    • <bdo id='5jOKo'></bdo><ul id='5jOKo'></ul>

    <small id='5jOKo'></small><noframes id='5jOKo'>

    <legend id='5jOKo'><style id='5jOKo'><dir id='5jOKo'><q id='5jOKo'></q></dir></style></legend>
    1. <tfoot id='5jOKo'></tfoot>

      1. Python中的Yaml合并

        时间:2023-09-13
          <legend id='W8V8a'><style id='W8V8a'><dir id='W8V8a'><q id='W8V8a'></q></dir></style></legend>

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

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

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

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

                • 本文介绍了Python中的Yaml合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以我正在玩弄让自己(当然还有任何关心使用它的人)在 Python 中为 Pygame 制作一个小样板库的想法.我想要一个系统,其中应用程序的设置随 yaml 文件一起提供.

                  So I'm toying around with the idea of making myself (and anyone who cares to use it of course) a little boilerplate library in Python for Pygame. I would like a system where settings for the application are provided with a yaml file.

                  所以我想如果库提供一个默认的 yaml 树并将它与用户提供的树合并它会很有用.出于可用性考虑,我想知道是否有任何人可以预测一个例程:

                  So I was thinking it would be useful if the library provided a default yaml tree and merged it with a user supplied one. For usability sake I wonder if possible there are any out there who can divine a routine where:

                  在树中用户提供的 yaml 与默认值重叠的任何情况下,用户提供的分支都会替换库提供的分支.

                  In any case in the tree where the user supplied yaml overlaps the default, the user supplied branches replace the library supplied ones.

                  在用户提供的 yaml 不与默认树重叠的任何情况下,默认树仍然存在.

                  In any case where the user supplied yaml does not overlap the default tree, the default tree persists.

                  用户提供的 yaml 提供的树中的任何多余分支都会被附加.

                  Any superflous branches in the tree provided by the user supplied yaml are appended.

                  我知道这个解释很冗长,因为我的要求可能很清楚.我想知道免费获得是不是有点多.

                  I know this explanation was verbose as it is probably clear what I'm asking for. I wonder if it is a bit much to get for free.

                  推荐答案

                  你可以使用 PyYAML 来解析文件,然后使用以下函数合并两棵树:

                  You could use PyYAML for parsing the files, and then the following function to merge two trees:

                  def merge(user, default):
                      if isinstance(user,dict) and isinstance(default,dict):
                          for k,v in default.iteritems():
                              if k not in user:
                                  user[k] = v
                              else:
                                  user[k] = merge(user[k],v)
                      return user
                  

                  或者,您可以在调用之前对用户树进行 deep-copy这个函数.

                  Optionally, you could do a deep-copy of the user-tree before calling this function.

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

                  上一篇:pyyaml:不带标签的倾销 下一篇:将 YAML 文件转换为 python dict

                  相关文章

                  最新文章

                        <bdo id='fK8oa'></bdo><ul id='fK8oa'></ul>
                      <legend id='fK8oa'><style id='fK8oa'><dir id='fK8oa'><q id='fK8oa'></q></dir></style></legend>
                    1. <small id='fK8oa'></small><noframes id='fK8oa'>

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