Python ConfigParser module rename a section

女生的网名这么多〃 提交于 2019-12-18 09:54:07

问题


How can you rename a section in a ConfigParser object?


回答1:


As far as I can tell, you need to

  • get the sections items via ConfigParser.items
  • remove the section via ConfigParser.remove_section
  • create a new section via ConfigParser.add_section
  • Put the items back into the new section via ConfigParser.set



回答2:


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)


来源:https://stackoverflow.com/questions/15069127/python-configparser-module-rename-a-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!