[英]python configparser writes configuration file to one line
I have a configuration file that I would like to change settings to from a python script. 我有一个配置文件,我想将设置从python脚本更改为。 Here is my skeleton code:
这是我的基本代码:
config = ConfigParser()
config.read('settings.conf')
config.set("SCRIPT", "SOMEFIELD", "%s"%SOMEVALUE)
config.write(open("settings.conf","wb"))
This works fine, however it writes everything to one line. 效果很好,但是将所有内容都写到了一行。 I might be being a bit picky, but would like to have new lines after each configurations field and section so that the file is human readable.
我可能会有点挑剔,但想在每个配置字段和部分之后添加新行,以便使文件易于阅读。
You are telling python to write a binary file ("b" char on 2nd arg of your open() call). 您正在告诉python编写一个二进制文件(open()调用的第二个arg上的“ b” char)。
Use 采用
config.write(open("settings.conf","w"))
Or better: 或更好:
with open("settings.conf","w") as settings_file:
config.write(settings_file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.