Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading