首页 > 编程语言 > 详细

python之configparser模块

时间:2017-09-23 14:13:59      阅读:253      评论:0      收藏:0      [点我收藏+]
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()

 

python之configparser模块

原文:http://www.cnblogs.com/zhaoxianglong1987/p/7580884.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!