python之configparser模块

from configparser import ConfigParser


def write():
    cp = ConfigParser()
    cp['one'] = {
        'aa': '111',
        'bb': '222'
    }
    cp['two'] = dict(
        cc='zxl',
        dd='hhu'
    )
    cp['three'] = {}
    cp['three']['ee'] = '888'
    t = cp['three']
    t['ff'] = '999'
    with open('config', 'w') as f:
        cp.write(f)


def read():
    cp = ConfigParser()
    # print(cp.sections())
    result = cp.read('config')
    # print(cp.has_option('one', 'aa'))
    # print(cp.has_section('three'))
    # for i in cp:
    #     print(i)
    # for i in cp['two']:
    #     print(i)
    # print(type(result), result)
    # print(cp.sections())
    # print('one' in cp)
    # print(cp['one'])
    # value = cp['one']['aa']
    # print(type(value), value)
    # value = cp.get('one', 'aa')
    # print(type(value), value)
    # value = cp.getint('one', 'aa')
    # print(type(value), value)


read()
def delete():
    cp = ConfigParser()
    read = cp.read('config')
    cp.remove_section('two')
    cp.remove_option('three', 'ee')
    with open('config2', 'w') as f:
        cp.write(f)


# delete()
def update():
    cp = ConfigParser()
    cp.read('config')
    cp.set('one', 'aa', 'zhangsan')
    with open('config', 'w') as f:
        cp.write(f)


# update()
原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7580884.html