简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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