Python ConfigParser模塊的使用示例
前言
在做項目的時候一些配置文件都會寫在settings配置文件中,今天在研究"州的先生"開源文檔寫作系統(tǒng)-MrDoc的時候,發(fā)現(xiàn)部分配置文件寫在config.ini中,并利用configparser進(jìn)行相關(guān)配置文件的讀取及修改。
一、ConfigParser模塊簡介
該模塊適用于配置文件的格式與windows ini文件類似,是用來讀取配置文件的包。配置文件的格式如下:中括號“[ ]”內(nèi)包含的為section。section 下面為類似于key-value 的配置內(nèi)容。格式如下:
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = Atlan [topsecret.server.com] Port = 50022 ForwardX11 = no
括號“[ ]”內(nèi)包含的為section。緊接著section 為類似于key-value 的options 的配置內(nèi)容。
二、ConfigParser模塊使用
1.寫入操作
代碼如下:
import configparser #引入模塊 config = configparser.ConfigParser() #類中一個方法 #實例化一個對象 config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' } #類似于操作字典的形式 config['bitbucket.org'] = {'User':'Atlan'} #類似于操作字典的形式 config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile) #將對象寫入文件 以上代碼做個簡單的解釋,和字典的操作方式相比,configparser模塊的操作方式,無非是在實例化的對象后面,跟一個section,在緊跟著設(shè)置section的屬性(類似字典的形式) config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' } #類似于操作字典的形式 #config后面跟的是一個section的名字,section的段的內(nèi)容的創(chuàng)建類似于創(chuàng)建字典。類似與字典當(dāng)然還有別的操作方式啦! config['bitbucket.org'] = {'User':'Atlan'} #類似于最經(jīng)典的字典操作方式
2.讀取操作
import configparser config = configparser.ConfigParser() #---------------------------查找文件內(nèi)容,基于字典的形式 print(config.sections()) # [] config.read('example.ini',encoding='utf-8') print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config) # False print('bitbucket.org' in config) # True print('DEFAULT' in config) # True print(config['bitbucket.org']["user"]) # Atlan print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default會默認(rèn)default的鍵 print(key) #user serveraliveinterval compression compressionlevel forwardx11 # 同for循環(huán),找到'bitbucket.org'下所有鍵 ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.options('bitbucket.org')) print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有鍵值對 [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'Atlan')] print(config.get('bitbucket.org','compression')) # yes get方法Section下的key對應(yīng)的value print(config.getboolean('bitbucket.org','compression')) # True
3.修改操作
import configparser config = configparser.ConfigParser() config.read('example.ini',encoding='utf-8') #讀文件 config.add_section('yuan') #添加section config.remove_section('bitbucket.org') #刪除section config.remove_option('topsecret.server.com',"forwardx11") #刪除一個配置項 # 修改某個option的值,如果不存在該option 則會創(chuàng)建 config.set('topsecret.server.com','k1','11111') config.set('yuan','k2','22222') #寫回文件 config.write(open("example.ini", "w")) # 寫到其他文件 with open('new2.ini','w') as f: config.write(f)
以上就是Python ConfigParser模塊的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Python ConfigParser模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Django應(yīng)用程序中如何發(fā)送電子郵件詳解
我們常常會用到一些發(fā)送郵件的功能,比如有人提交了應(yīng)聘的表單,可以向HR的郵箱發(fā)郵件,這樣,HR不看網(wǎng)站就可以知道有人在網(wǎng)站上提交了應(yīng)聘信息。下面這篇文章就介紹了在Django應(yīng)用程序中如何發(fā)送電子郵件的相關(guān)資料,需要的朋友可以參考借鑒。2017-02-02python中實現(xiàn)php的var_dump函數(shù)功能
這篇文章主要介紹了python中實現(xiàn)php的var_dump函數(shù)功能,var_dump函數(shù)在PHP中調(diào)試時非常實用,本文介紹在Python中實現(xiàn)這個函數(shù),需要的朋友可以參考下2015-01-01matplotlib設(shè)置顏色、標(biāo)記、線條,讓你的圖像更加豐富(推薦)
這篇文章主要介紹了matplotlib設(shè)置顏色、標(biāo)記、線條,讓你的圖像更加豐富,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03用python監(jiān)控服務(wù)器的cpu,磁盤空間,內(nèi)存,超過郵件報警
這篇文章主要介紹了如果用python監(jiān)控服務(wù)器的cpu,磁盤空間,內(nèi)存,超過郵件報警,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01