python - 如何在python中使用 ":"而不是 "="编写.cfg文件

标签 python string config

我正在使用 python3 ConfigParser,我的代码是

conf = configparser.ConfigParser()
cfg_file = open('./config.cfg', 'w')
conf.set(None, 'a', '0')
conf.write(cfg_file)
cfg_file.close()

我预计 config.cfg 文件中的结果为 a : 0。但是,我得到了a = 0。有没有办法将“=”替换为“:”?

最佳答案

使用分隔符参数来 ConfigParser :

When `delimiters' is given, it will be used as the set of substrings that divide keys from values.

conf = configparser.ConfigParser(delimiters=':')

结果:

[DEFAULT]
a : 0

关于python - 如何在python中使用 ":"而不是 "="编写.cfg文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73703632/

相关文章:

swift - 为什么 string.contains ("text") 不适用于 swift 5 中的字符串?

C++(真正)安全的标准字符串搜索?

git - 如何通过命令行参数覆盖 Git 配置选项?

.htaccess - 如何根据获取环境变量动态设置AngularjS基本URL?

Python Pexpect 生成对象 Flush

c++ - 如何在 MPI 中发送 std::string?

Python 父类检查子类中要在类函数中使用的属性

python - 在 config.py 中导入值

python - Django 项目的 Heroku 日志丢失错误

python - 如何获得两个字典的对称差异