Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有一种简单的方法可以在 python 中使用 ConfigParser 重命名配置文件中的部分?我宁愿不必删除该部分并重新创建它,但这是我现在唯一的答案。
不,内置的 ConfigParser 将部分存储为_sections,这是一个字典。由于这是 python,您可以访问该变量以进行简单的复制。(config._sections[new_name] = config._sections[old_name]; config._sections.pop(old_name)
_sections
config._sections[new_name] = config._sections[old_name]; config._sections.pop(old_name)
但是 ConfigParser 可能会在以后更改,这会破坏您的实现。
因此我不建议这样做,或者您可以继承 ConfigParser 并实现 delete_section 或更改选项的存储方式。