According to the ConfigParser documentation,
Configuration files may include comments, prefix characters (# and;). Comments may appear on their own, otherwise an empty line or may be entered into lines containing values or section names. In the latter case, they must be preceded by spaces character, which will be recognized as a comment. (For backward compatibility, only; launches the inline comment, but # does not.)
If you want to read a “comment” with a value, you can skip the space before the character ; or use # . But in this case, the lines comment1 and comment2 become part of the value and are no longer considered comments.
A better approach would be to use another property name, such as variable1_comment , or to define another section in the comment configuration:
[local] variable1 = value1 [comments] variable1 = comment1
The first solution requires that you generate a new key using another (i.e., calculate variable1_comment from variable1 ), and the other - use the same key for different sections in the configuration file.
As with Python 2.7.2, you can always read a comment along a line if you use the # character. As the docs say, for backward compatibility. The following code should work smoothly:
config = ConfigParser.ConfigParser() config.read('config.ini') assert config.get('local', 'variable1') == 'value1' assert config.get('local', 'variable2') == 'value2 # comment2'
for the following config.ini file:
[local] variable1 = value1 ; comment1 variable2 = value2
If you make this decision, be sure to manually analyze the get() result for the values and comments.