ConfigParser 解析配置文件详述

参见:https://docs.python.org/2/library/configparser.html?highlight=configparser#configparser-objects

1. ConfigParser介绍

提示:python2.x中 使用ConfigParser,python3.x中使用configparser.,当将你的源码直接使用python3时,可自动转换导入。

ConfigParser是一个解析配置文件类,用来读取配置文件。只要你的配置文件与windows ini文件类似, 那么你就可以使用ConfigParser进行解析。

注意事项

  • 配置文件是由一个或者多个节section构成,每个节sections可由键:值组成, 也可以键=值这种形式
  • section区分大小写,而key不区分大小写
  • 键值默认将头尾的空白符去掉
  • 值可以多行
  • 配置文件可以添加注释,注释以#或;为开头
  • configparser有default_section的概念,默认为[DEFAULT]节,也就是之后的所有的section都有该默认section中的键值对

2. ConfigParser示例

示例1:
  • 其中myini.conf配置如下
[db]
db = test_db
host = 127.0.0.1
port = 8001
user = test
passwd = 123456

[other]
db: test_db2
host: 168.23.0.1
uset: test
passwd: 123456

[default]
key: test_default
  • 其中myConfigTest.py配置如下
# -*- coding: utf-8 -*-
import ConfigParser
import os

def main():
    """main func """
    # 获取配置文件的路径
    path = os.getcwd() + '/myini.conf'
    # 创建ConfigParser实例
    config = ConfigParser.ConfigParser()
    # 读取配置文件
    config.read(path)
    # 列举所有sections, 注意不包含DEFAULT
    sections = config.sections()
    print "sections: ", sections
    # 获取db info
    db = config.get('db', 'db')
    host = config.get('db', 'host')
    port = config.get('db', 'port')
    user = config.get('db', 'user')
    passwd = config.get('db', 'passwd')
    print "******* db info *******"
    print "db: ", db, \
            "\nhost: ", host, \
            "\nport: ", port, \
            "\npasswd: ", passwd
    print "**********************"
    # 返回other序列
    other = config.options('other')
    print "other: ", other
    # 返回default 序列
    default = config.options('default')
    print "default: ", default

if __name__ == '__main__':
    main()
  • 执行结果


  • ConfigParser.read(filename) : 读取配置文件
  • ConfigParser.write(filename) : 写入配置文件
  • ConfigParser.sections() : 返回配置文件中所有的节(section)序列,注:不包含DEFAULT(示例2详细解释)
  • ConfigParser.get(section,option) : 返回特定section下某个键的值
  • ConfigParser.options(section) : 返回特定section下所有键
    -ConfigParser.set(section, option, value): 设置特定section下option的值为val
    -ConfigParser.items(section): 返回特定下section下(键,值)对
示例2:

下面我们来讲述当section为DEFAULT这种情况
搬运工 https://codeday.me/bug/20180607/177700.html

先抛结论:在[DEFAULT]部分中的内容都会被传播到其他每个section

升级前
  • myini.conf 配置如下:
[server 1]
host = 127.0.0.1
user = user1
passwd = passwd1

[server 2]
host: 127.0.0.1
user: user2
passwd: passwd2
  • myConfigTest.py如下:
# -*- coding: utf-8 -*-
import ConfigParser
import os

def main():
    """main func """
    # 获取配置文件的路径
    path = os.getcwd() + '/myini.conf'
    # 创建ConfigParser实例
    config = ConfigParser.ConfigParser()
    # 读取配置文件
    config.read(path)
    # 列举所有sections, 注意不包含DEFAULT
    sections = config.sections()
    print "sections: ", sections
    # 返回server 1序列
    server1 = config.items('server 1')
    print "server1: ", server1
    # 返回default 序列
    server2 = config.items('server 2')
    print "server2: ", server2

if __name__ == '__main__':
    main()

测试结果:
server 1server 2打印结果与配置文件设置键值对一致

升级后
  • myini.conf 配置如下:
    增加[DEFAULT]节,且[server 1],[server 2] 无host键值对,myConfigTest.py无升级,沿用升级前脚本
[DEFAULT]
host = 168.1.1.1

[server 1]
user = user1
passwd = passwd1

[server 2]
user: user2
passwd: passwd2

测试结果:
(1) 验证了[server 1],[server 2] 默认继承了[DEFAULT]节的键值
(2) 验证了ConfigParser.sections()默认不显示[DEFAULT]

你可能感兴趣的:(ConfigParser 解析配置文件详述)