Python-ConfigParser常用操作示例

configParser 模块用于操作配置文件
配置文件的格式和windows的ini文件类似,可以包含一个或者多个节(section),每个节又可以包含多个参数(键值对形式: key=value)

ConfigParser 常用方法

1、config=ConfigParser.ConfigParser()  
创建ConfigParser实例  

2、config.sections()  --> list
返回配置文件中所有节的序列

3、config.options(section)  --> list
返回某个项目中的所有键的序列  

4、config.get(section,option)  --> str
返回section节中,option的键值  

5、config.add_section(str)  
添加一个配置文件节(str)  

6、config.set(section,option,val)  
设置section节中,键名为option的值(val)  

7、config.read(filename)  
读取配置文件  

8、config.write(obj_file)  
写入配置文件  

综合示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser

def writeConfig(filename):
    '''
    写配置文件
    配置文件全路径名:param filename:
    '''
    config = ConfigParser.ConfigParser()

    section_name = "section_1"
    config.add_section(section_name)
    config.set(section_name, "key_1", "value_1_1")
    config.set(section_name, "key_2", "value_1_2")
    config.set(section_name, "key_3", "value_1_3")

    section_name = "section_2"
    config.add_section(section_name)
    config.set(section_name, "key_1", "value_2_1")
    config.set(section_name, "key_2", "value_2_2")
    config.set(section_name, "key_3", "value_2_3")

    config.write(open(filename, "w"))

def updateConfig(filename, section, **keyv):
    '''
    修改配置文件
    文件名:param filename:
    section节:param section:
    配置键值对(key=value):param keyv:
    :return:
    '''
    config = ConfigParser.ConfigParser()
    config.read(filename)
    for key in keyv:
        config.set(section, key, keyv[key])
    config.write(open(filename, "w"))

def printConfig(filename):
    '''
    打印配置文件信息
    配置文件全路径名:param filename:
    '''
    config = ConfigParser.ConfigParser()
    config.read(filename)
    sections = config.sections()
    print "sections:", sections
    for section in sections:
        print "[%s]" % section
        for option in config.options(section):
            print "\t%s=%s" % (option, config.get(section, option))

if __name__ == '__main__':
    file_name = 'test.ini'
    writeConfig(file_name)
    printConfig(file_name)
    updateConfig(file_name, 'section_2', key_2='修改了')
    print "修改后:"
    printConfig(file_name)
    print "end"

执行结果

sections: ['section_1', 'section_2']
[section_1]
    key_1=value_1_1
    key_2=value_1_2
    key_3=value_1_3
[section_2]
    key_1=value_2_1
    key_2=value_2_2
    key_3=value_2_3
修改后:
sections: ['section_1', 'section_2']
[section_1]
    key_1=value_1_1
    key_2=value_1_2
    key_3=value_1_3
[section_2]
    key_1=value_2_1
    key_2=修改了
    key_3=value_2_3
end

test.ini文件

[section_1]
key_1 = value_1_1
key_2 = value_1_2
key_3 = value_1_3

[section_2]
key_1 = value_2_1
key_2 = 修改了
key_3 = value_2_3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值