I'm using configparser and Python 3.3 to create a script which makes some small modifications to config files. These config files often contain commented-out lines, like so:

[Section]
Value1=Foo
;Very important comment
;Another very important comment
Value2=Bar

Understandably, Python strips these out when it writes the new version of the config file, but that's something of a problem. Is there any way to copy the comments into the new config file? (Unfortunately, order is also important. Thanks to the OrderedDict, the order of config options is maintained properly, but the order of comments will need to be maintained as well, which hopefully will not be an issue.)

Thanks!

有帮助吗?

解决方案

Here is a simple trick/workaround: Instead of a comment, put REM_x= in front to turn the comment into an option (which your code will ignore):

[Section]
Value1=Foo
REM_1=Very important comment
REM_2=Another very important comment
REM_Value2=Bar
Value2=Baz
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top