Python中ConfigParser模塊示例詳解
1. 簡介
有些時(shí)候在項(xiàng)目中,使用配置文件來配置一些靈活的參數(shù)是比較常見的事,因?yàn)檫@會(huì)使得代碼的維護(hù)變得更方便。而ini配置文件是比較常用的一種,今天介紹用ConfigParser模塊來解析ini配置文件。
2. ini配置文件格式
# 這是注釋 ; 這也是注釋 [section1] name = wang age = 18 heigth = 180 [section2] name = python age = 19
3. 讀取ini文件
configparser模塊為Python自帶模塊不需要單獨(dú)安裝,但要注意,在Python3中的導(dǎo)入方式與Python2的有點(diǎn)小區(qū)別
# python2 import ConfigParser # python3 import configparser
3.1 初始化對(duì)象并讀取文件
import configparser import os # 創(chuàng)建對(duì)象 config = configparser.ConfigParser() dirPath = os.path.dirname(os.path.realpath(__file__)) inipath = os.path.join(dirPath,'test.ini') # 讀取配置文件,如果配置文件不存在則創(chuàng)建 config.read(inipath,encoding='utf-8')
3.2 獲取并打印所有節(jié)點(diǎn)名稱
secs = config.sections() print(secs)
輸出結(jié)果:
['section1', 'section2']
3.3 獲取指定節(jié)點(diǎn)的所有key
option = config.options('section1') print(option)
輸出結(jié)果:
['name', 'age', 'heigth']
3.4 獲取指定節(jié)點(diǎn)的鍵值對(duì)
item_list = config.items('section2') print(item_list)
輸出結(jié)果:
[('name', 'python'), ('age', '19')]
3.5 獲取指定節(jié)點(diǎn)的指定key的value
val = config.get('section1','age') print('section1的age值為:',val)
輸出結(jié)果:
section1的age值為: 18
3.6 將獲取到值轉(zhuǎn)換為int\bool\浮點(diǎn)型
Attributes = config.getint('section2','age') print(type(config.get('section2','age'))) print(type(Attributes)) # Attributes2 = config.getboolean('section2','age') # Attributes3 = config.getfloat('section2','age')
輸出結(jié)果:
<class 'str'>
<class 'int'>
3.7 檢查section或option是否存在,返回bool值
has_sec = config.has_section('section1') print(has_sec) has_opt = config.has_option('section1','name') print(has_opt)
輸出結(jié)果:
TrueTrue
3.8 添加一個(gè)section和option
if not config.has_section('node1'): config.add_section('node1') # 不需判斷key存不存在,如果key不存在則新增,若已存在,則修改value config.set('section1','weight','100') # 將添加的節(jié)點(diǎn)node1寫入配置文件 config.write(open(inipath,'w')) print(config.sections()) print(config.options('section1'))
輸出結(jié)果:
['section1', 'section2', 'node1']
[('name', 'wang'), ('age', '18'), ('heigth', '180'), ('weight', '100')]
3.9 刪除section和option
# 刪除option print('刪除前的option:',config.items('node1')) config.remove_option('node1','dd') # 將刪除節(jié)點(diǎn)node1后的內(nèi)容寫回配置文件 config.write(open(inipath,'w')) print('刪除后的option:',config.items('node1'))
輸出結(jié)果:
刪除前的option: [('dd', 'ab')]
刪除后的option: []
# 刪除section print('刪除前的section: ',config.sections()) config.remove_section('node1') config.write(open(inipath,'w')) print('刪除后的section: ',config.sections())
輸出結(jié)果:
刪除前的section: ['section1', 'section2', 'node1']
刪除后的section: ['section1', 'section2']
3.10 寫入方式
1、write寫入有兩種方式,一種是刪除源文件內(nèi)容,重新寫入:w
config.write(open(inipath,'w'))
另一種是在原文基礎(chǔ)上繼續(xù)寫入內(nèi)容,追加模式寫入:a
config.write(open(inipath,'a'))
需要注意的是,config.read(inipath,encoding='utf-8')
只是將文件內(nèi)容讀取到內(nèi)存中,即使經(jīng)過一系列的增刪改操作,只有執(zhí)行了以上的寫入代碼后,操作過的內(nèi)容才會(huì)被寫回文件,才能生效。
到此這篇關(guān)于Python中ConfigParser模塊詳談的文章就介紹到這了,更多相關(guān)Python中ConfigParser模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中排序函數(shù)sorted()函數(shù)的使用實(shí)例
sorted()作為Python內(nèi)置函數(shù)之一,其功能是對(duì)序列(列表、元組、字典、集合、還包括字符串)進(jìn)行排序,下面這篇文章主要給大家介紹了關(guān)于Python中排序函數(shù)sorted()函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-11-11Python數(shù)據(jù)分析之?Pandas?Dataframe條件篩選遍歷詳情
這篇文章主要介紹了Python數(shù)據(jù)分析之?Pandas?Dataframe條件篩選遍歷詳情,查詢Pandas?Dataframe數(shù)據(jù)時(shí),經(jīng)常會(huì)篩選出符合條件的數(shù)據(jù),關(guān)于其使用方式,需要的小伙伴可以參考一下下面文章內(nèi)容2022-05-05Python?虛擬機(jī)字典dict內(nèi)存優(yōu)化方法解析
這篇文章主要為大家介紹了Python?虛擬機(jī)字典dict內(nèi)存優(yōu)化方法解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法
今天小編就為大家分享一篇matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05