python之读取配置文件模块configparser(一)基本操作

configparser模块是读取类ini文件使用,其有固定的读取格式如下:

[section1]
option11 = value11
option12 = value12
....
[section2]
option21 = value21
option22 = value22
...

假如我们有一个config.ini文件结构如下:

[IP]
port = 8808
address = http://sdv.functest.com
[password]
globalMD5 = functest

通过下面的代码来分别说明configparser的增删改查

1、查询

用下面代码来实现:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
cp = configparser.ConfigParser()
cp.read(filepath)
sections
= cp.sections()#查询配置文件中所有section,即节点的值 options = cp.options('IP')#查询某个section下所有的参数 items = cp.items('IP')#查询某个section下所有的键值对 get_port = cp.get('IP','port')#查询某个section下某个参数的值,若需要int或者float也可以直接使用geint或者getfloat或者getboolean get_address = cp.get('IP','address') get_globalMD5 = cp.get('password','globalMD5') print('sections:',sections) print('options_IP:',options) print('items_IP:',items) print('port:',get_port) print('address:',get_address) print('globalMD5:',get_globalMD5)

得到的结果如下:

sections: ['IP', 'password']
options_IP: ['port', 'address']
items_IP: [('port', '8808'), ('address', 'http://sdv.functest.com')]
port: 8808
address: http://sdv.functest.com
globalMD5: functest

这里要说明下getboolean可以获取包括yes/no,on/off,true/false,1/0等值yes,on,true和1标识True,不区分大小写 

另外,获取配置文件并不一定需要get,也可以有另外一种写法,如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
print(filepath)
cp = configparser.ConfigParser()
cp.read(filepath)

#打印section
for sec in cp:
    print(sec)
#打印IP中的option
for opt in cp['IP']:
    print(opt)
#获取globalMD5的值
get_globalMD5 = cp['password']['globalMD5']

print('globalMD5:',get_globalMD5)

上述代码执行结果如下:

D:PycharmProjectsuntitledMyTestProjectMyLearnconfig.ini
DEFAULT
IP
password
port
address
globalMD5: functest

可以看到section中多了个DEFAULT,这个是默认section,和configparser.ConfigParser的参数default_section有关,后面会讲到

2、修改

修改主要用set来实现,代码如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
cp = configparser.ConfigParser()
cp.read(filepath)


#修改
cp.set('IP','port','9900')#修改当前section中option的值,这里也可以使用cp['IP']['port'] = '9900'来写
with open(filepath,'w+') as f:
    cp.write(f)

这个是最简单的,只要设置了对应参数的值,然后保存下即可,修改后的config.ini文件如下:

[IP]
port = 9900
address = http://sdv.functest.com

[password]
globalmd5 = functest

和查询类似,这边set也可以类似get直接写成如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
print(filepath)
cp = configparser.ConfigParser(default_section='password')
cp.read(filepath)

get_globalMD5_1 = cp['password']['globalMD5']
cp['password']['globalMD5'] = 'test'
with open(filepath,'w+') as f:
    cp.write(f)
get_globalMD5_2 = cp['password']['globalMD5']

print('globalMD5_1:',get_globalMD5_1)
print('globalMD5_2:',get_globalMD5_2)

执行结果如下:

globalMD5_1: functest
globalMD5_2: test

3、增加

增加有两类:一是增加section,一个是增加option,都是比较简单的操作,代码实现如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
cp = configparser.ConfigParser()
cp.read(filepath)

#增加
cp.add_section('addtest1')#增加一个section
cp.add_section('addtest2')
cp.set('addtest1','country','china')#若已经存在对应option则为修改,若不存在则为增加
cp.set('addtest1','province','jiangsu')
cp.set('addtest2','country','china')
cp.set('addtest2','province','shandong')
with open(filepath,'w+') as f:
cp.write(f)

其实也很简单set既可作修改也可以做增加使用,执行后config.ini文件如下:

[IP]
port = 9900
address = http://sdv.functest.com

[password]
globalmd5 = functest

[addtest1]
country = china
province = jiangsu

[addtest2]
country = china
province = shandong

4、删除

删除有俩个操作,一个是删除section同时会删除section下的所有option,另外一个是删除option,代码如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
cp = configparser.ConfigParser()
cp.read(filepath)

#删除
cp.remove_option('addtest1','province')#删除option
cp.remove_section('addtest2')#删除section
with open(filepath,'w+') as f:
    cp.write(f)

python就是这么简单,只要符合格式,操作就很简单,执行结果如下:

[IP]
port = 9900
address = http://sdv.functest.com

[password]
globalmd5 = functest

[addtest1]
country = china

addtest2已经全部删除,addtest1中只剩下country这个option

5、判断配置文件中是否有对应的section或option

代码如下:

import configparser
import os

filepath = os.path.join(os.getcwd(),'config.ini')
cp = configparser.ConfigParser()
cp.read(filepath)

#判断是否存在section
if cp.has_section('IP'):
    print('has IP section')
else:
    print('has not IP section')

if cp.has_section('LLL'):
    print('has LLL section')
else:
    print('has not LLL section')

#判断是否存在option
if cp.has_option('IP','port'):
    print('has port option')
else:
    print('has not port option')

if cp.has_option('IP','url'):
    print('has url option')
else:
    print('has not url option')

执行结果如下:

has IP section
has not LLL section
has port option
has not url option

以上结果是符合预期的

原文地址:https://www.cnblogs.com/watertaro/p/10539408.html