Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用configparser将不同的配置部分写入不同的ini文件?_Python_Python 3.x - Fatal编程技术网

Python 如何使用configparser将不同的配置部分写入不同的ini文件?

Python 如何使用configparser将不同的配置部分写入不同的ini文件?,python,python-3.x,Python,Python 3.x,好的,我正计划使用configparser模块创建一些ini配置文件,不过我遇到了一些麻烦 如何将配置的某些部分写入不同的ini文件 我已经阅读了所有关于它的官方文件,似乎没有看到任何允许这样做的东西 有什么建议吗?您可以定义一个ConfigParser类的子类,并重写它的write方法。例如: class MySafeConfigParser(ConfigParser.SafeConfigParser): def write(self, fileobject, *sections):

好的,我正计划使用configparser模块创建一些ini配置文件,不过我遇到了一些麻烦

如何将配置的某些部分写入不同的ini文件

我已经阅读了所有关于它的官方文件,似乎没有看到任何允许这样做的东西


有什么建议吗?

您可以定义一个
ConfigParser
类的子类,并重写它的
write
方法。例如:

class MySafeConfigParser(ConfigParser.SafeConfigParser):
    def write(self, fileobject, *sections):
        'Write an .ini-format representation of the specified sections'
        temp = ConfigParser.SafeConfigParser(self.defaults())
        for s in sections:
            temp.add_section(s)
            for i, v in self.items(s):
                temp.set(s, i, v)
        temp.write(fileobject)
我可以用以下方法测试我的子类:

config = MySafeConfigParser({ 'foo': '5.0' })

config.add_section('Section1')
config.set('Section1', 'foo', '3.1415')
config.set('Section1', 'bar', 'baz')

config.add_section('Section2')
config.set('Section2', 'abc', 'def')
config.set('Section2', 'ghi', 'jkl')

config.add_section('Section3')
config.set('Section3', 'mno', 'pqr')
config.set('Section3', 'stu', 'vwx')

with open('section1.ini', 'wb') as f:
    config.write(f, 'Section1')

with open('sections2and3.ini', 'wb') as f:
    config.write(f, 'Section2', 'Section3')
运行此代码后,文件
section1.ini
包含:

[DEFAULT]
foo = 5.0

[Section1]
foo = 3.1415
bar = baz
[DEFAULT]
foo = 5.0

[Section2]
foo = 5.0
abc = def
ghi = jkl

[Section3]
foo = 5.0
mno = pqr
stu = vwx
第2节和第3节。ini
包含:

[DEFAULT]
foo = 5.0

[Section1]
foo = 3.1415
bar = baz
[DEFAULT]
foo = 5.0

[Section2]
foo = 5.0
abc = def
ghi = jkl

[Section3]
foo = 5.0
mno = pqr
stu = vwx
最重要的是,
ConfigParser.read(…)
方法已经接受多个文件名,因此我可以使用以下方法重新读取两个配置文件:

config = MySafeConfigParser()
config.read(['section1.ini', 'sections2and3.ini'])
Edit:我没有注意到原来的问题是关于Python 3的,因此下面是上面转换为Python 3的代码:

class MySafeConfigParser(configparser.ConfigParser):
    def write(self, fileobject, *sections, space_around_delimiters = True):
        'Write an .ini-format representation of the specified sections'
        temp = configparser.ConfigParser(self.defaults())
        for s in sections:
            temp.add_section(s)
            for i, v in self.items(s):
                temp.set(s, i, v)
        temp.write(fileobject, space_around_delimiters)
config = MySafeConfigParser({ 'foo': '5.0' })

config['Section1'] = {}
config['Section1']['foo'] = '3.1415'
config['Section1']['bar'] = 'baz'

config['Section2'] = {}
config['Section2']['abc'] = 'def'
config['Section2']['ghi'] = 'jkl'

config['Section3'] = {}
config['Section3']['mno'] = 'pqr'
config['Section3']['stu'] = 'vwx'

with open('section1.ini', 'w') as f:
    config.write(f, 'Section1')

with open('sections2and3.ini', 'w') as f:
    config.write(f, 'Section2', 'Section3')
以及转换为Python 3的测试代码:

class MySafeConfigParser(configparser.ConfigParser):
    def write(self, fileobject, *sections, space_around_delimiters = True):
        'Write an .ini-format representation of the specified sections'
        temp = configparser.ConfigParser(self.defaults())
        for s in sections:
            temp.add_section(s)
            for i, v in self.items(s):
                temp.set(s, i, v)
        temp.write(fileobject, space_around_delimiters)
config = MySafeConfigParser({ 'foo': '5.0' })

config['Section1'] = {}
config['Section1']['foo'] = '3.1415'
config['Section1']['bar'] = 'baz'

config['Section2'] = {}
config['Section2']['abc'] = 'def'
config['Section2']['ghi'] = 'jkl'

config['Section3'] = {}
config['Section3']['mno'] = 'pqr'
config['Section3']['stu'] = 'vwx'

with open('section1.ini', 'w') as f:
    config.write(f, 'Section1')

with open('sections2and3.ini', 'w') as f:
    config.write(f, 'Section2', 'Section3')

给我们举一个你想要达到的目标的例子。