将ConfigParser.items('')转换为字典

如何将ConfigParser.items('section')的结果转换为字典来格式化string,如下所示:

import ConfigParser config = ConfigParser.ConfigParser() config.read('conf.ini') connection_string = ("dbname='%(dbname)s' user='%(dbuser)s' host='%(host)s' " "password='%(password)s' port='%(port)s'") print connection_string % config.items('db') 

这实际上已经在config._sections为你完成了。 例:

 $ cat test.ini [First Section] var = value key = item [Second Section] othervar = othervalue otherkey = otheritem 

接着:

 >>> from ConfigParser import ConfigParser >>> config = ConfigParser() >>> config.read('test.ini') >>> config._sections {'First Section': {'var': 'value', '__name__': 'First Section', 'key': 'item'}, 'Second Section': {'__name__': 'Second Section', 'otherkey': 'otheritem', 'othervar': 'othervalue'}} >>> config._sections['First Section'] {'var': 'value', '__name__': 'First Section', 'key': 'item'} 

编辑:我解决同样的问题是downvoted所以我会进一步说明我的答案做同样的事情,而不必通过dict() ,因为config._sections 模块已经为您提供

示例test.ini:

 [db] dbname = testdb dbuser = test_user host = localhost password = abc123 port = 3306 

魔术发生:

 >>> config.read('test.ini') ['test.ini'] >>> config._sections {'db': {'dbname': 'testdb', 'host': 'localhost', 'dbuser': 'test_user', '__name__': 'db', 'password': 'abc123', 'port': '3306'}} >>> connection_string = "dbname='%(dbname)s' user='%(dbuser)s' host='%(host)s' password='%(password)s' port='%(port)s'" >>> connection_string % config._sections['db'] "dbname='testdb' user='test_user' host='localhost' password='abc123' port='3306'" 

所以这个解决scheme没有错,实际上只需要less一步。 感谢您的阻止!

你有没有尝试过

 print connection_string % dict(config.items('db')) 

我是如何做到的?

 my_config_parser_dict = {s:dict(config.items(s)) for s in config.sections()} 

没有其他的答案,但当它不是你的方法的真正业务,你只需要在一个地方使用较less的行,并采取字典压缩的力量可能是有用的。

我知道很久以前就有人提出这个问题,并且select了一个解决scheme,但所选解决scheme没有考虑到默认值和variablesreplace。 由于这是search从parsing器创build字典的第一个命中,我以为我会通过使用ConfigParser.items()发布我的解决scheme,其中包括默认和variablesreplace。

 from ConfigParser import SafeConfigParser defaults = {'kone': 'oneval', 'ktwo': 'twoval'} parser = SafeConfigParser(defaults=defaults) parser.set('section1', 'kone', 'new-val-one') parser.add_section('section1') parser.set('section1', 'kone', 'new-val-one') parser.get('section1', 'ktwo') parser.add_section('section2') parser.get('section2', 'kone') parser.set('section2', 'kthree', 'threeval') parser.items('section2') thedict = {} for section in parser.sections(): thedict[section] = {} for key, val in parser.items(section): thedict[section][key] = val thedict {'section2': {'ktwo': 'twoval', 'kthree': 'threeval', 'kone': 'oneval'}, 'section1': {'ktwo': 'twoval', 'kone': 'new-val-one'}} 

一个方便的function来做到这一点可能看起来像这样:

 def as_dict(config): """ Converts a ConfigParser object into a dictionary. The resulting dictionary has sections as keys which point to a dict of the sections options as key => value pairs. """ the_dict = {} for section in config.sections(): the_dict[section] = {} for key, val in config.items(section): the_dict[section][key] = val return the_dict 

对于一个单独的部分,比如“一般”,你可以这样做:

 dict(parser['general'])