如何重命名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.