简体   繁体   English

python configparser写入默认值部分

[英]python configparser writing to defaults section

i'm using the configparser module in python to read and write some .ini style files. 我在python中使用configparser模块读取和写入一些.ini样式文件。 i want to be able to create and write to the DEFAULTS section, however, it appears to be hardcoded to not allow the creation of such a section. 我希望能够创建和写入DEFAULTS部分,但是,似乎已对其进行了硬编码,以不允许创建此类部分。

is it possible? 可能吗? or even advised to do this? 甚至建议这样做?

You don't have to create the DEFAULT section, it already exists. 您不必创建DEFAULT节,它已经存在。 You can set values in it right away. 您可以立即在其中设置值。

config = ConfigParser.RawConfigParser()
config.set('DEFAULT', 'name2', 'value2')
with open('file.conf', 'wb') as cf:
    config.write(cf)

The values you set as defaults when creating the ConfigParser instance will also get written to the DEFAULT section, as wim noted. 如您所知,您在创建ConfigParser实例时设置为默认值的值也将写入DEFAULT部分。

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

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