Python 2.7 configparser在写入时正在删除注释

Python 2.7 configparser在写入时正在删除注释,python-2.7,Python 2.7,在我的Python程序中,我读取了一个配置(从文件中读取真实代码),但在编写时,注释就消失了。我怎么能把它放在里面 import sys import ConfigParser import io sample_config = """ [user01] name = Sal # update this value password = abc123 """ config = ConfigParser.RawConfigParser(allow_no_value=True) config.r

在我的Python程序中,我读取了一个配置(从文件中读取真实代码),但在编写时,注释就消失了。我怎么能把它放在里面

import sys
import ConfigParser
import io

sample_config = """
[user01]
name = Sal
# update this value
password = abc123
"""

config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
config.set('user01', 'password', '123abc')
config.write(sys.stdout)
输出:

[user01]
name = Sal
password = 123abc
我用它在你读和写的时候保留评论 配置文件:

from configobj import ConfigObj

sample_config = """
[user01]
name = Sal
# update this value
password = abc123
"""

config = ConfigObj(sample_config.splitlines())
config['user01']['password'] = '123abc'
config.write(sys.stdout)