简体   繁体   English

Python ConfigParser:如何计算在特定部分中设置的选项(而不是默认值)

[英]Python ConfigParser: how to work out options set in a specific section (rather than defaults)

I have a config file that I read using the RawConfigParser in the standard ConfigParser library. 我有一个使用标准ConfigParser库中的RawConfigParser读取的配置文件。 My config file has a [DEFAULT] section followed by a [specific] section. 我的配置文件有一个[DEFAULT]部分,然后是[specific]部分。 When I loop through the options in the [specific] section, it includes the ones under [DEFAULT], which is what is meant to happen. 当我循环浏览[specific]部分中的选项时,它包括[DEFAULT]下的选项,这就是要发生的情况。

However, for reporting I wanted to know whether the option had been set in the [specific] section or in [DEFAULT]. 但是,对于报告,我想知道是在[特定]部分还是[默认]中设置了该选项。 Is there any way of doing that with the interface of RawConfigParser, or do I have no option but to parse the file manually? 是否可以通过RawConfigParser的接口来执行此操作,或者我只能手动解析文件,所以别无选择吗? (I have looked for a bit and I'm starting to fear the worst ...) (我已经寻找了一点,我开始担心最糟糕的事情...)

For example 例如

[DEFAULT] [默认]

name = a 名称= a

surname = b 姓= b

[SECTION] [部分]

name = b 名称= b

age = 23 年龄= 23

How do you know, using RawConfigParser interface, whether options name & surname are loaded from section [DEFAULT] or section [SECTION]? 您如何使用RawConfigParser界面从[DEFAULT]部分或[SECTION]部分中加载选项名称和姓氏?

(I know that [DEFAULT] is meant to apply to all, but you may want to report things like this internally in order to work your way through complex config files) (我知道[DEFAULT]适用于所有对象,但您可能希望在内部报告类似的内容,以便通过复杂的配置文件进行操作)

thanks! 谢谢!

I did this recently by making the options into dictionaries, and then merging the dictionaries. 我最近通过将选项制作成字典,然后合并字典来做到这一点。 The neat thing about it is that the user parameters override the defaults, and it's easy to pass them all to a function. 整洁的是,用户参数会覆盖默认值,并且很容易将它们全部传递给函数。

import ConfigParser
config = ConfigParser.ConfigParser()
config.read('config.ini')

defaultparam = {k:v for k,v in config.items('DEFAULT')}
userparam = {k:v for k,v in config.items('Section 1')}

mergedparam = dict(defaultparam.items() + userparam.items())

Given this configuration file: 给定此配置文件:

[DEFAULT]
name = a
surname = b

[Section 1]
name  = section 1 name
age = 23
#we should get a surname value from defaults

[Section 2]
name = section 2 name
surname = section 2 surname
age = 24

Here's a program that can understand that Section 1 is using the default surname property. 这是一个可以理解第1节正在使用默认姓氏属性的程序。

import ConfigParser

parser = ConfigParser.RawConfigParser()
parser.read("config.ini")
#Do your normal config processing here
#When it comes time to audit default vs. explicit,
#clear the defaults
parser._defaults = {}
#Now you will see which options were explicitly defined
print parser.options("Section 1")
print parser.options("Section 2")

And here's the output: 这是输出:

['age', 'name']
['age', 'surname', 'name']

RawConfigParser.has_option(section, option)完成这项工作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM