• python ConfigParser 模块


    格式:

    [section]
    option = value

    import ConfigParser

    config=ConfigParser.ConfigParser()

    #读
    config.read('test.cfg')
    #with open('test.cfg','r') as f:
    #  config.readfp(f,'test.cfg')

    print config.sections() #获取所有的section
    print config.has_section(section) #判断是否有section
    print config.has_option(section,option) #判断是否有option
    print config.options(section) #获取section里面的所有的option
    print config.items(section) #获取section里面的所有的(option,value)键值对
    print config.get(section,option) #获取value

    #写
    config.add_section('section') #添加一个section
    config.set('section1','name','aaa') #添加section的option,value
    with open('test.cfg','a+') as f:
      config.write(f) #写入文件


    #删除
    config.read('test.cfg')
    config.remove_section('section')
    config.remove_option('section','option')
    with open('test.cfg','w') as f:
      config.write(f)

  • 相关阅读:
    hdu多校4
    hdu多校第三场
    牛客多校4
    bzoj 1477 扩展欧几里德
    bzoj 1485 卡特兰数 + 分解因子
    hdu多校 2
    牛客网暑期多校2
    bzoj 1040 基向内环树dp
    hdu 多校第一场
    SPOJ
  • 原文地址:https://www.cnblogs.com/xia-dong/p/11752344.html
Copyright © 2020-2023  润新知