如何将ConfigParser.items('section')的结果转换为字典以格式化字符串,如下所示:
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')
Run Code Online (Sandbox Code Playgroud)
Ian*_*and 93
你有没有尝试过
print connection_string % dict(config.items('db'))
Run Code Online (Sandbox Code Playgroud)
?
jat*_*ism 51
这实际上已经为你完成了config._sections.例:
$ cat test.ini
[First Section]
var = value
key = item
[Second Section]
othervar = othervalue
otherkey = otheritem
Run Code Online (Sandbox Code Playgroud)
然后:
>>> 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'}
Run Code Online (Sandbox Code Playgroud)
编辑: 我的解决方案相同的问题downvoted所以我会进一步说明我的答案是如何做同样的事情,而不必直通部分dict(),因为config._sections是由模块为您已经提供.
示例test.ini:
[db]
dbname = testdb
dbuser = test_user
host = localhost
password = abc123
port = 3306
Run Code Online (Sandbox Code Playgroud)
魔术发生:
>>> 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'"
Run Code Online (Sandbox Code Playgroud)
所以这个解决方案没有错,实际上只需要少一步.谢谢你的到来!
Mic*_*ico 48
我是如何在一行中完成的.
my_config_parser_dict = {s:dict(config.items(s)) for s in config.sections()}
Run Code Online (Sandbox Code Playgroud)
仅仅是其他答案,但当它不是你的方法的真正业务,你只需要在一个地方使用更少的行,并采取dict理解的力量可能是有用的.
Jam*_*yle 15
我知道这是很久以前问的,并且选择了一个解决方案,但所选的解决方案没有考虑默认值和变量替换.因为它是搜索从解析器创建dicts时的第一个命中,我想我会发布我的解决方案,其中包含使用ConfigParser.items()的默认和可变替换.
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'}}
Run Code Online (Sandbox Code Playgroud)
执行此操作的便利功能可能如下所示:
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
Run Code Online (Sandbox Code Playgroud)
在 Python +3.6 中你可以这样做
文件.ini
[SECTION1]
one = 1
two = 2
[SECTION2]
foo = Hello
bar = World
[SECTION3]
param1 = parameter one
param2 = parameter two
Run Code Online (Sandbox Code Playgroud)
文件.py
[SECTION1]
one = 1
two = 2
[SECTION2]
foo = Hello
bar = World
[SECTION3]
param1 = parameter one
param2 = parameter two
Run Code Online (Sandbox Code Playgroud)
如果您需要列出的所有部分,您应该使用cfg.sections()
小智 6
另一种选择是:
配置文件
[DEFAULT]
potato=3
[foo]
foor_property=y
potato=4
[bar]
bar_property=y
Run Code Online (Sandbox Code Playgroud)
解析器.py
import configparser
from typing import Dict
def to_dict(config: configparser.ConfigParser) -> Dict[str, Dict[str, str]]:
"""
function converts a ConfigParser structure into a nested dict
Each section name is a first level key in the the dict, and the key values of the section
becomes the dict in the second level
{
'section_name': {
'key': 'value'
}
}
:param config: the ConfigParser with the file already loaded
:return: a nested dict
"""
return {section_name: dict(config[section_name]) for section_name in config.sections()}
Run Code Online (Sandbox Code Playgroud)
主要.py
import configparser
from parser import to_dict
def main():
config = configparser.ConfigParser()
# By default section names are parsed to lower case, optionxform = str sets to no conversion.
# For more information: https://docs.python.org/3/library/configparser.html#configparser-objects
# config.optionxform = str
config.read('config.ini')
print(f'Config read: {to_dict(config)}')
print(f'Defaults read: {config.defaults()}')
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)