简体   繁体   English

Python ConfigParser 模块重命名一个部分

[英]Python ConfigParser module rename a section

如何重命名ConfigParser对象中的部分?

example helper function - silly really but it might save someone a few minutes work...示例辅助函数 - 真的很傻,但它可能会为某人节省几分钟的工作......

def rename_section(cp, section_from, section_to):
    items = cp.items(section_from)
    cp.add_section(section_to)
    for item in items:
        cp.set(section_to, item[0], item[1])
    cp.remove_section(section_from)

As far as I can tell, you need to据我所知,你需要

You can use the private method _sections to rename it in a single line.您可以使用私有方法_sections在一行中重命名它。

def rename_config_section(config, old_section, new_section):
    """Rename a section in a configparser object."""
    config._sections[new_section] = config._sections.pop(old_section)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM