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

Question:

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 ":"?

Asked By: RafaelDD

||

Answers:

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
Answered By: rdas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.