如何使用python一次性加载配置属性而不传递节名

2024-06-28 11:56:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的方法:

def readConfigProperties(sectionName):
    # Using Confir Parser : Import the package First : add interpreter too
    config = configparser.RawConfigParser()
    config.read('Path To Properties ')
    details_dict = dict(config.items(sectionName))
    print(details_dict)
    return details_dict

目前,我正在传递节名,这可以正常工作,但我想在场景之前一次加载完整的属性文件


Tags: the方法importaddconfigparserpackagedef
2条回答

config.sections()将返回一个节列表,因此,例如,如果要将整个配置文件读入嵌套字典,可以使用:

{name: dict(config.items(name)) for name in config.sections()}

输入文件中的示例:

[blah]
foo = bar

[baz]
quux = whatever

这将给你:

{'blah': {'foo': 'bar'}, 'baz': {'quux': 'whatever'}}

做什么呢

all = {} 
for section_name in config.sections():
    for name, value in config.items(section_name):
        all[name] =value
 

相关问题 更多 >