Python3中configparser模塊讀寫ini文件并解析配置的用法詳解
Python3中configparser模塊簡介
configparser 是 Pyhton 標(biāo)準(zhǔn)庫中用來解析配置文件的模塊,并且內(nèi)置方法和字典非常接近。Python2.x 中名為 ConfigParser,3.x 已更名小寫,并加入了一些新功能。
配置文件的格式如下:
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = Tom [topsecret.com] Port: 50022 ForwardX11: no
“[ ]”包含的為 section,section 下面為類似于 key - value 的配置內(nèi)容;
configparser 默認(rèn)支持 ‘=' ‘:' 兩種分隔。
configparser 常用方法
初始化實(shí)例
使用 configparser 首先需要初始化實(shí)例,并讀取配置文件:
>>> import configparser
>>> config = configparser.ConfigParser() # 注意大小寫
>>> config.read("config.ini") # 配置文件的路徑
["config.ini"]
或者可以直接讀字典
>>> parser = configparser.ConfigParser()
>>> parser.read_dict({'section1': {'key1': 'value1',
... 'key2': 'value2',
... 'key3': 'value3'},
... 'section2': {'keyA': 'valueA',
... 'keyB': 'valueB',
... 'keyC': 'valueC'},
... 'section3': {'foo': 'x',
... 'bar': 'y',
... 'baz': 'z'}
... })
獲取所有 sections
>>> config.sections() ['bitbucket.org', 'topsecret.com'] # 注意會(huì)過濾掉[DEFAULT]
獲取指定 section 的 keys & values
>>> config.items('topsecret.com')
>>>> [('port', '50022'), ('forwardx11', 'no')] # 注意items()返回的字符串會(huì)全變成小寫
獲取指定 section 的 keys
>>> config.options('topsecret.com')
['Port', 'ForwardX11']
>>> for option in config['topsecret.com']: ... print(option) Port ForwardX11
獲取指定 key 的 value
>>> config['bitbucket.org']['User'] 'Tom'
>>> config.get('bitbucket.org', 'User')
'Tom'
>>> config.getint('topsecret.com', 'Port')
50022
configparser模塊檢查
>>> 'DEFAULT' in config True >>> 'test' in config['section_test'] False >>> 'Tom' in config['bitbucket.org']['User'] True
>>> config.has_section('bitbucket.org')
True
>>> config.has_option('section_test', 'test')
False
configparser模塊添加
>>> config.add_section('Section_1')
>>> config.set('Section_1', 'key_1', 'value_1') # 注意鍵值是用set()方法
>>> config.write(open('config.ini', 'w')) # 一定要寫入才生效
configparser模塊刪除
>>> config.remove_option('Section_1', 'key_1')
True
>>> config.remove_section('Section_1')
True
>>> config.clear() # 清空除[DEFAULT]之外所有內(nèi)容
>>> config.write(open('config.ini', 'w'))
關(guān)于 [DEFAULT]
[DEFAULT] 一般包含 ini 格式配置文件的默認(rèn)項(xiàng),所以 configparser 部分方法會(huì)自動(dòng)跳過這個(gè) section 。
前面已經(jīng)提到 sections() 是獲取不到的,還有刪除方法對(duì) [DEFAULT] 也無效:
>>> config.remove_section('DEFAULT')
False
>>> config.clear()
>>> 'DEFAULT' in config
True
>>> 'ForwardX11' in config['DEFAULT']
True
>>> config.sections()
[]
但指定刪除和修改 [DEFAULT] 里的 keys & values 是可以的:
>>> config.remove_option('DEFAULT', 'ForwardX11')
True
>>> config.set('DEFAULT', 'ForwardX11','no')
>>> config['DEFAULT']['ForwardX11']
'no'
還有個(gè)特殊的是,has_section() 也無效,可以和 in 區(qū)別使用
>>> config.has_section('DEFAULT')
False
>>> 'DEFAULT' in config
True
更多關(guān)于Python3中configparser模塊讀寫ini文件并解析配置的用法請(qǐng)查看下面的相關(guān)鏈接
- Python?configparser模塊的用法示例代碼
- Python中Parser的超詳細(xì)用法實(shí)例
- python中parser.add_argument()用法實(shí)例(命令行選項(xiàng)、參數(shù)和子命令解析器)
- Python Parser的用法
- Python ArgumentParse的subparser用法說明
- Python3.5內(nèi)置模塊之shelve模塊、xml模塊、configparser模塊、hashlib、hmac模塊用法分析
- Python HTML解析模塊HTMLParser用法分析【爬蟲工具】
- Python中optparser庫用法實(shí)例詳解
- python命令行參數(shù)解析OptionParser類用法實(shí)例
- Python中Parser的用法小結(jié)
相關(guān)文章
淺談tensorflow語義分割api的使用(deeplab訓(xùn)練cityscapes)
這篇文章主要介紹了淺談tensorflow語義分割api的使用(deeplab訓(xùn)練cityscapes),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python+selenium實(shí)現(xiàn)趣頭條的視頻自動(dòng)上傳與發(fā)布
本文主要介紹了通過Python+selenium實(shí)現(xiàn)趣頭條的短視頻自動(dòng)上傳與發(fā)布功能,同時(shí)支持抖音、快手、b站、小紅書等平臺(tái)的視頻自動(dòng)化同步發(fā)布。需要的朋友可以參考一下2021-12-12
TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式
這篇文章主要介紹了TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用pycharm連接讀取orcl數(shù)據(jù)庫的表的操作方法
這篇文章主要介紹了使用pycharm連接讀取orcl數(shù)據(jù)庫的表的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
解決python錯(cuò)誤提示:TypeError: expected string or&nb
這篇文章主要介紹了解決python錯(cuò)誤提示:TypeError: expected string or bytes-lik問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
python中精確的浮點(diǎn)數(shù)運(yùn)算示例
這篇文章主要為大家介紹了python中精確的浮點(diǎn)數(shù)運(yùn)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

