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

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

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

        在 Python 中使用设置文件的最佳做法是什么?

        时间:2023-09-12
        • <bdo id='QqVvH'></bdo><ul id='QqVvH'></ul>

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

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

                  <legend id='QqVvH'><style id='QqVvH'><dir id='QqVvH'><q id='QqVvH'></q></dir></style></legend>
                • 本文介绍了在 Python 中使用设置文件的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个带有很多参数的命令行脚本.我现在已经到了一个论点太多的地步,我也想有一些字典形式的论点.

                  I have a command line script that I run with a lot of arguments. I have now come to a point where I have too many arguments, and I want to have some arguments in dictionary form too.

                  所以为了简化事情,我想用设置文件来运行脚本.我真的不知道要使用哪些库来解析文件.这样做的最佳做法是什么?我当然可以自己敲出一些东西,但是如果有一些图书馆可以解决这个问题,我会全力以赴.

                  So in order to simplify things I would like to run the script with a settings file instead. I don't really know what libraries to use for the parsing of the file. What's the best practice for doing this? I could of course hammer something out myself, but if there is some library for this, I'm all ears.

                  一些要求":

                  • 而不是使用 pickle,我希望它是一个易于阅读和编辑的简单文本文件.
                  • 我希望能够在其中添加类似字典的数据,即应该支持某种形式的嵌套.
                  • Rather than using pickle I would like it to be a straight forward text file that can easily be read and edited.
                  • I want to be able to add dictionary-like data in it, i.e., some form of nesting should be supported.

                  一个简化的伪示例文件:

                  A simplified pseudo example file:

                  truck:
                      color: blue
                      brand: ford
                  city: new york
                  cabriolet:
                      color: black
                      engine:
                          cylinders: 8
                          placement: mid
                      doors: 2
                  

                  推荐答案

                  你可以有一个普通的 Python 模块,比如 config.py,像这样:

                  You can have a regular Python module, say config.py, like this:

                  truck = dict(
                      color = 'blue',
                      brand = 'ford',
                  )
                  city = 'new york'
                  cabriolet = dict(
                      color = 'black',
                      engine = dict(
                          cylinders = 8,
                          placement = 'mid',
                      ),
                      doors = 2,
                  )
                  

                  并像这样使用它:

                  import config
                  print(config.truck['color'])  
                  

                  这篇关于在 Python 中使用设置文件的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用python读取fortran无格式文件 下一篇:如何将 YAML 文件解析/读取到 Python 对象中?

                  相关文章

                  最新文章

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

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