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:\PycharmProjects\untitled\MyTestProject\MyLearn\config.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

以上結果是符合預期的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM