in Education by
I can use the ConfigParser module in python to create ini-files using the methods add_section and set (see sample in http://docs.python.org/library/configparser.html). But I don't see anything about adding comments. Is that possible? I know about using # and ; but how to get the ConfigParser object to add that for me? I don't see anything about this in the docs for configparser. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If you want to get rid of the trailing =, you can subclass ConfigParser.ConfigParser as suggested by atomocopter and implement your own write method to replace the original one: import sys import ConfigParser class ConfigParserWithComments(ConfigParser.ConfigParser): def add_comment(self, section, comment): self.set(section, '; %s' % (comment,), None) def write(self, fp): """Write an .ini-format representation of the configuration state.""" if self._defaults: fp.write("[%s]\n" % ConfigParser.DEFAULTSECT) for (key, value) in self._defaults.items(): self._write_item(fp, key, value) fp.write("\n") for section in self._sections: fp.write("[%s]\n" % section) for (key, value) in self._sections[section].items(): self._write_item(fp, key, value) fp.write("\n") def _write_item(self, fp, key, value): if key.startswith(';') and value is None: fp.write("%s\n" % (key,)) else: fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) config = ConfigParserWithComments() config.add_section('Section') config.set('Section', 'key', 'value') config.add_comment('Section', 'this is the comment') config.write(sys.stdout) The output of this script is: [Section] key = value ; this is the comment Notes: If you use an option name whose name starts with ; and value is set to None, it will be considered a comment. This will let you add comments and write them to files, but not read them back. To do that, you'll have implement your own _read method that takes care of parsing comments and maybe add a comments method to make it possible to get the comments for each section.

Related questions

0 votes
    I am the Python newbie. I would like to make the code well commented. For example, in C I can naturally write ... 't find the solution. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    When I try to generate a model with acumos I get this error : File "/Users/fredericchantrel/.pyenv/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    Is this possible? From what I'm seeing, the only way to get options into the the python class is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I have a given list of string and a list of characters and I want to check the string with ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    How can I remove emojis that start with '\x' when reading a csv file using pandas in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want to encode an image into a string using the base64 module. I've ran into a problem though. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I need to create a hollow pyramid with a solid top layer like so: Height: 5 * *** * * * ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Is it possible to use any chart modules with wxpython? And are there any good ones out there? I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    Is it possible to use any chart modules with wxpython? And are there any good ones out there? I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    Is it possible to use any chart modules with wxpython? And are there any good ones out there? I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Is it possible to use any chart modules with wxpython? And are there any good ones out there? I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I am using Eclipse with Pydev and googleAppengine. I have python 2.7 installed. I am trying to run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
...