Python configparser模塊常用方法解析
ConfigParser模塊在python中用來(lái)讀取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一個(gè)或多個(gè)節(jié)(section), 每個(gè)節(jié)可以有多個(gè)參數(shù)(鍵=值)。使用的配置文件的好處就是不用在程序員寫死,可以使程序更靈活。
注意:在python 3 中ConfigParser模塊名已更名為configparser
configparser函數(shù)常用方法:
讀取配置文件:
read(filename) #讀取配置文件,直接讀取ini文件內(nèi)容
sections() #獲取ini文件內(nèi)所有的section,以列表形式返回['logging', 'mysql']
options(sections) #獲取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']
items(sections) #獲取指定section下所有的鍵值對(duì),[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
get(section, option) #獲取section中option的值,返回為string類型
>>>>>獲取指定的section下的option <class 'str'> 127.0.0.1getint(section,option) 返回int類型
getfloat(section, option) 返回float類型
getboolean(section,option) 返回boolen類型
舉例如下:
配置文件ini如下:
[logging]
level = 20
path =
server =[mysql]
host=127.0.0.1
port=3306
user=root
password=123456
注意,也可以使用:替換=
代碼如下:
import configparser
from until.file_system import get_init_path
conf = configparser.ConfigParser()
file_path = get_init_path()
print('file_path :',file_path)
conf.read(file_path)
sections = conf.sections()
print('獲取配置文件所有的section', sections)
options = conf.options('mysql')
print('獲取指定section下所有option', options)
items = conf.items('mysql')
print('獲取指定section下所有的鍵值對(duì)', items)
value = conf.get('mysql', 'host')
print('獲取指定的section下的option', type(value), value)
運(yùn)行結(jié)果如下:
file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
獲取配置文件所有的section ['logging', 'mysql']
獲取指定section下所有option ['host', 'port', 'user', 'password']
獲取指定section下所有的鍵值對(duì) [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
獲取指定的section下的option <class 'str'> 127.0.0.1
綜合使用方法:
import configparser
"""
讀取配置文件信息
"""
class ConfigParser():
config_dic = {}
@classmethod
def get_config(cls, sector, item):
value = None
try:
value = cls.config_dic[sector][item]
except KeyError:
cf = configparser.ConfigParser()
cf.read('settings.ini', encoding='utf8') #注意setting.ini配置文件的路徑
value = cf.get(sector, item)
cls.config_dic = value
finally:
return value
if __name__ == '__main__':
con = ConfigParser()
res = con.get_config('logging', 'level')
print(res)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
好用的Python編輯器WingIDE的使用經(jīng)驗(yàn)總結(jié)
WingIDE是個(gè)專為python程序語(yǔ)言設(shè)計(jì)的集成開發(fā)環(huán)境。從1999年起,Wingware公司便開始專注于python開發(fā),目前WingIDE已經(jīng)是著名的python開發(fā)框架,面向項(xiàng)目風(fēng)格的 IDE 對(duì)于大型產(chǎn)品非常有用, 是個(gè)很有前途的開發(fā)環(huán)境。2016-08-08
Centos Python2 升級(jí)到Python3的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇Centos Python2 升級(jí)到Python3的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
關(guān)于Python中定制類的比較運(yùn)算實(shí)例
今天小編就為大家分享一篇關(guān)于Python中定制類的比較運(yùn)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
keras實(shí)現(xiàn)圖像預(yù)處理并生成一個(gè)generator的案例
這篇文章主要介紹了keras實(shí)現(xiàn)圖像預(yù)處理并生成一個(gè)generator的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python實(shí)現(xiàn)的計(jì)算馬氏距離算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的計(jì)算馬氏距離算法,簡(jiǎn)單說(shuō)明了馬氏距離算法原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用馬氏距離算法的相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例
本篇文章主要介紹了Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

