Python 使用ConfigParser创建包含文件中指定的所有节和元素的配置类

Python 使用ConfigParser创建包含文件中指定的所有节和元素的配置类,python,python-2.7,configparser,Python,Python 2.7,Configparser,我对stackoverflow一无所知,事实上我找到了一个解决我兴奋问题的方法:。在我的例子中,pillmuncher给出的解析几乎是完美的。但是我需要区分各个部分,而不是每次都覆盖相应的值 是否有一种方法可以完成相同的类,但可以区分节和名称 示例输入(配置文件): pillmuncher的类别: from ConfigParser import SafeConfigParser section_names = 'x_axis', 'y_axis' class MyConfiguratio

我对stackoverflow一无所知,事实上我找到了一个解决我兴奋问题的方法:。在我的例子中,pillmuncher给出的解析几乎是完美的。但是我需要区分各个部分,而不是每次都覆盖相应的值

是否有一种方法可以完成相同的类,但可以区分节和名称

示例输入(配置文件):

pillmuncher的类别:

from ConfigParser import SafeConfigParser


section_names = 'x_axis', 'y_axis'

class MyConfiguration(object):

def __init__(self, *file_names):
    parser = SafeConfigParser()
    parser.optionxform = str  # make option names case sensitive
    found = parser.read(file_names)
    if not found:
        raise ValueError('No config file found!')
    for name in section_names:
        self.__dict__.update(parser.items(name))  # <-- here the magic happens

config = MyConfiguration('my.cfg', 'other.cfg')
从ConfigParser导入SafeConfigParser
截面名称='x_轴','y_轴'
类MyConfiguration(对象):
定义初始化(self,*文件名):
parser=SafeConfigParser()
parser.optionxform=str#使选项名称区分大小写
found=parser.read(文件名)
如果未找到:
raise VALUERROR('未找到配置文件!')
对于“名称”部分中的名称:

self.u dict.u.update(parser.items(name))#请提供示例输入和预期行为。请提供示例输入和预期行为。
from ConfigParser import SafeConfigParser


section_names = 'x_axis', 'y_axis'

class MyConfiguration(object):

def __init__(self, *file_names):
    parser = SafeConfigParser()
    parser.optionxform = str  # make option names case sensitive
    found = parser.read(file_names)
    if not found:
        raise ValueError('No config file found!')
    for name in section_names:
        self.__dict__.update(parser.items(name))  # <-- here the magic happens

config = MyConfiguration('my.cfg', 'other.cfg')