简体   繁体   中英

How to write a .cfg file with ":" instead of "=" in python

I'm using python3 ConfigParser, my code is

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

I expected the result to be a: 0 in the config.cfg file. However, I got a = 0 . Is there a way to replace "=" with ":"?

Use the delimiters parameters to ConfigParser :

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

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

Result:

[DEFAULT]
a : 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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