在python中使用ConfigParser重命名config.ini部分 [英] Rename config.ini section using ConfigParser in python

查看:107
本文介绍了在python中使用ConfigParser重命名config.ini部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使用python中的ConfigParser重命名配置文件中的部分?我希望不必删除该部分并重新创建它,但这是我目前唯一的答案。

Is there an easy way to rename a section in a config file using ConfigParser in python? I'd prefer not to have to delete the section and recreate it, but that is my only answer right now.

推荐答案

否。内置的ConfigParser将节存储为 _sections ,这是一个命令。由于这是python,因此您可以访问该变量以进行简单复制。
config._sections [new_name] = config._sections [old_name]; config._sections.pop(old_name)

No. The builtin ConfigParser stores sections as _sections, which is a dict. Since this is python you could access that variable to do an easy copy. (config._sections[new_name] = config._sections[old_name]; config._sections.pop(old_name)

但是ConfigParser可能会在以后更改,这会破坏您的实现。

But ConfigParser may change at some later date and this would break your implementation.

因此,我不建议您这样做,也可以将ConfigParser和实现delete_section或更改选项的存储方式。

Therefore I don't recommend doing that, alternatively you could subclass ConfigParser and implement a delete_section or change the way options are stored.

这篇关于在python中使用ConfigParser重命名config.ini部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆