python-2.7 – 在Python 2.7中手动构建ConfigParser的深层副本

刚开始我的
Python学习曲线,并将一些代码移植到Python 2.7.看起来在Python 2.7中,不再可能对ConfigParser的实例执行deepcopy().似乎Python团队对恢复这样的功能并不十分感兴趣:

http://bugs.python.org/issue16058

有人可以提出一个优雅的解决方案来手动构建ConfigParser实例的深度复制/复制吗?

非常感谢,-Pete

解决方法

基于@Toenex答案,针对Python 2.7进行了修改:

import StringIO
import ConfigParser

# Create a deep copy of the configuration object
config_string = StringIO.StringIO()
base_config.write(config_string)

# We must reset the buffer to make it ready for reading.        
config_string.seek(0)        
new_config = ConfigParser.ConfigParser()
new_config.readfp(config_string)

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。