python-2.7 – 在Python 2.7中手动构建ConfigParser的深层副本
发布时间:2020-12-16 23:14:53 所属栏目:Python 来源:网络整理
导读:刚开始我的 Python学习曲线,并将一些代码移植到Python 2.7.看起来在Python 2.7中,不再可能对ConfigParser的实例执行deepcopy().似乎Python团队对恢复这样的功能并不十分感兴趣: http://bugs.python.org/issue16058 有人可以提出一个优雅的解决方案来手动构
刚开始我的
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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |