【问题标题】:Read all the contents in ini file into dictionary with Python使用Python将ini文件中的所有内容读入字典
【发布时间】:2011-03-14 08:03:45
【问题描述】:

通常,我编写如下代码以获取变量中的特定项目,如下所示

try:
    config = ConfigParser.ConfigParser()
    config.read(self.iniPathName)
except ConfigParser.MissingSectionHeaderError, e:
    raise WrongIniFormatError(`e`)

try:
    self.makeDB = config.get("DB","makeDB")
except ConfigParser.NoOptionError:
    self.makeDB = 0

有没有办法读取python字典中的所有内容?

例如

[一种] x=1 y=2 z=3 [乙] x=1 y=2 z=3

被写入

val["A"]["x"] = 1 ... val["B"]["z"] = 3

【问题讨论】:

  • 来自 Python 3 的 ConfigParser 具有此功能,您可以使用 backport 在 Python 2 中拥有它

标签: python configuration-files ini


【解决方案1】:

我设法得到了答案,但我希望应该有更好的答案。

dictionary = {}
for section in config.sections():
    dictionary[section] = {}
    for option in config.options(section):
        dictionary[section][option] = config.get(section, option)

【讨论】:

  • 我认为这是一个很好的解决方案,你为什么不满意呢?
  • 这应该是答案,因为它无需使用“private”“_sections”属性即可解决问题。当然,如果需要使用 OrderedDict,只需使用它来代替常规 dict。
  • defaultdict(dict) 替换 dictionary 将删除中间 dict 创建。
【解决方案2】:

ConfigParser 的实例数据在内部存储为嵌套字典。您可以直接复制它,而不是重新创建它。

>>> import ConfigParser
>>> p = ConfigParser.ConfigParser()
>>> p.read("sample_config.ini")
['sample_config.ini']
>>> p.__dict__
{'_defaults': {}, '_sections': {'A': {'y': '2', '__name__': 'A', 'z': '3', 'x': '1'}, 'B':         {'y': '2', '__name__': 'B', 'z': '3', 'x': '1'}}, '_dict': <type 'dict'>}
>>> d = p.__dict__['_sections'].copy()
>>> d
{'A': {'y': '2', '__name__': 'A', 'z': '3', 'x': '1'}, 'B': {'y': '2', '__name__': 'B', 'z': '3', 'x': '1'}}

编辑:

Alex Martelli 的solution 更干净、更健壮、更漂亮。虽然这是公认的答案,但我建议改用他的方法。有关详细信息,请参阅他对此解决方案的评论。

【讨论】:

  • 我总是不愿访问受保护的(“下划线开头”)属性名称(以及通过__dict__ 的荒谬复杂性根本没有帮助——d=p._sections.copy()完全等价,更简单,更直接)。这就是为什么我在回答中建议改用子类的替代方法——子类期望访问基类的受保护属性。在 C++ 中,这是强制执行的;在 Python 中,它不是,但那是因为用户应该受到足够的纪律,而不是需要强制执行;-)。
【解决方案3】:

我建议子类化ConfigParser.ConfigParser(或SafeConfigParser,&c)以安全地访问“受保护”属性(以单个下划线开头的名称——“私有”将以两个下划线开头的名称,即使在子类中也不能访问...):

import ConfigParser

class MyParser(ConfigParser.ConfigParser):

    def as_dict(self):
        d = dict(self._sections)
        for k in d:
            d[k] = dict(self._defaults, **d[k])
            d[k].pop('__name__', None)
        return d

这模拟了配置解析器的通常逻辑,并保证在所有有 ConfigParser.py 模块的 Python 版本中工作(最高 2.7,这是 2.* 系列的最后一个 - 知道会有没有未来的 Python 2.any 版本是如何保证兼容性;-)。

如果您需要支持未来的 Python 3.* 版本(最高到 3.1 并且可能即将推出的 3.2 应该没问题,只需将模块重命名为全小写 configparser 而不是当然)它可能需要一些注意/几年后会有所调整,但我预计不会有什么重大的变化。

【讨论】:

    【解决方案4】:

    如何在py中解析ini文件?

    import ConfigParser
    config = ConfigParser.ConfigParser()
    config.read('/var/tmp/test.ini')
    print config.get('DEFAULT', 'network')
    

    test.ini 文件所在的位置:

    [DEFAULT]
    network=shutup
    others=talk
    

    【讨论】:

      【解决方案5】:

      我知道这个问题是 5 年前提出的,但今天我已经把这个听写理解变成了一件事情:

      parser = ConfigParser()
      parser.read(filename)
      confdict = {section: dict(parser.items(section)) for section in parser.sections()}
      

      【讨论】:

        【解决方案6】:

        需要注意的另一件事是,ConfigParser 将键值转换为小写,因此如果您将配置条目转换为字典,请交叉检查您的要求。因为这个,我遇到了一个问题。对我来说,我有驼峰式键,因此当我开始使用字典而不是文件时,不得不更改一些代码。 ConfigParser.get() 方法在内部将密钥转换为小写。

        【讨论】:

          【解决方案7】:

          来自https://wiki.python.org/moin/ConfigParserExamples

          def ConfigSectionMap(section):
          dict1 = {}
          options = Config.options(section)
          for option in options:
              try:
                  dict1[option] = Config.get(section, option)
                  if dict1[option] == -1:
                      DebugPrint("skip: %s" % option)
              except:
                  print("exception on %s!" % option)
                  dict1[option] = None
          return dict1
          

          【讨论】:

            【解决方案8】:

            假设文件:config.properties 包含以下内容:

            • k =v
            • k2= v2
            • k3= v3

            python 代码:

            def read_config_file(file_path):
                    with open(file=file_path, mode='r') as fs:
                        return {k.strip(): v.strip() for i in [l for l in fs.readlines() if l.strip() != ''] for k, v in [i.split('=')]}
            
            
            print('file as dic: ', read_config_file('config.properties'))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-11-28
              • 1970-01-01
              • 1970-01-01
              • 2013-12-23
              相关资源
              最近更新 更多