python configparser模块

转载自:https://xin053.github.io/2016/07/18/configparser配置文件解析器使用详解/

configparser简介

本文介绍python3中configparser模块的使用,configparser模块是用来解析ini配置文件的解析器,关于ini配置文件的结构可以看python官方文档中的介绍:

ini文件结构

ini文件结构需要注意一下几点:

  • 键值对可用=或者:进行分隔
  • section的名字是区分大小写的,而key的名字是不区分大小写的
  • 键值对中头部和尾部的空白符会被去掉
  • 值可以为多行
  • 配置文件可以包含注释,注释以#或者;为前缀

注意:configparser有 default_section 的概念,默认为[DEFAULT]节,也就是之后的所有的 section都包含该默认section中的键值对,详情参见configparser源码的__init__()方法

基本使用

为了创建如下ini文件:

configparser 模块主要使用 ConfigParser 类来解析 ini 文件

[DEFAULT]  # section
ServerAliveInterval = 45  # key-value,option
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no

我们可以使用如下代码:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ServerAliveInterval': '45',
...                      'Compression': 'yes',
...                      'CompressionLevel': '9'}
>>> config['bitbucket.org'] = {}
>>> config['bitbucket.org']['User'] = 'hg'
>>> config['topsecret.server.com'] = {}
>>> topsecret = config['topsecret.server.com']
>>> topsecret['Port'] = '50022'     # mutates the parser
>>> topsecret['ForwardX11'] = 'no'  # same here
>>> config['DEFAULT']['ForwardX11'] = 'yes'
>>> with open('example.ini', 'w') as configfile:
...   config.write(configfile)

然后我们再读取该 ini 文件:

>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

除了可以使用列表的方式获取值,也可以通过section级别的get()方法获取,同时该函数可以指定默认值

>>> topsecret.get('Port')
'50022'
>>> topsecret.get('CompressionLevel')
'9'
>>> topsecret.get('Cipher', '3des-cbc')
'3des-cbc'

而解析器级别的get()函数的默认值是通过fallback参数指定的:

>>> config.get('bitbucket.org', 'monster',
...            fallback='No such things as monsters')
'No such things as monsters'

需要注意的是,无论是通过列表方式获取值,还是通过get()方法获取值,获取到的数据都字符串类型,如果想要获取指定类型的数据,可以使用如下的几个方法:

  • getint()
  • getfloat()
  • getboolean()

同时需要注意getboolean()方法能判断 True/False 的值有: ‘yes’/‘no’, ‘on’/‘off’, ‘true’/‘false’ 和 ‘1’/‘0’

Interpolation

创建 ConfigParser() 类的时候可以指定 interpolation 参数,如果将 interpolation 设置为BasicInterpolation(),则配置文件中的 %(key)s 结构会被解析,比如example.ini文件内容如下:

[Paths]
home_dir: /Users
my_dir: %(home_dir)s/lumberjack
my_pictures: %(my_dir)s/Pictures
>>> import configparser
>>> config = configparser.ConfigParser(interpolation=configparser.BasicInterpolation())
>>> config.read(r'F:\coding\python\example.ini')
['F:\\coding\\python\\example.ini']
>>> config['Paths']['my_dir']
'/Users/lumberjack'

可以看到 %(home_dir)s 被解析成了/Users,说白了,相当于配置文件中的变量,创建ConfigParser()类的时候如果没有指定interpolation参数,则不会解析%(key)s,只会返回字符串而已

当然Interpolation还有更高级的使用方法,创建 ConfigParser() 类的时候指定 interpolation 参数为ExtendedInterpolation(),那么解析器会解析 ${section:key} 结构,那么上面的 ini 文件应该写成如下的格式:

[Paths]
home_dir: /Users
my_dir: ${home_dir}/lumberjack
my_pictures: ${my_dir}/Pictures

并且 ExtendedInterpolation() 也能解析更复杂的,像下面这样的ini文件:

[Common]
home_dir: /Users
library_dir: /Library
system_dir: /System
macports_dir: /opt/local
[Frameworks]
Python: 3.2
path: ${Common:system_dir}/Library/Frameworks/
[Arthur]
nickname: Two Sheds
last_name: Jackson
my_dir: ${Common:home_dir}/twosheds
my_pictures: ${my_dir}/Pictures
python_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python}

ConfigParser

ConfigParser对象的其他方法,如:

  • add_section(section)
  • has_section(section)
  • options(section)
  • set(section,optiion,value) # 设置section某个option的值,不存在此option则Error,存在则修改value
  • has_option(section, option)
  • remove_option(section, option)
  • remove_section(section)
  • 不要忘了:config.write(open('example.ini','w'))
posted @ 2019-11-02 13:08  wztshine  阅读(361)  评论(0编辑  收藏  举报