Python操作配置文件ini的三種方法講解
python 操作配置文件ini的三種方法
方法一:crudini 命令
說(shuō)明
crudini命令是Linux下的一個(gè)操作配置文件的命令工具
用法
crudini --set [--existing] config_file section [param] [value] # 修改配置文件內(nèi)容 crudini --get [--format=sh|ini] config_file [section] [param] # 獲取配置文件內(nèi)容 crudini --del [--existing] config_file section [param] # 刪除配置文件內(nèi)容 crudini --merge [--existing] config_file [section] # 合并
舉例
添加
crudini --set test.ini test_section test_param test_value
更新
crudini --set [--existing] test.ini test_section test_param test_value
刪除
刪除param:
crudini --del test.ini test_section test_param
刪除section:
crudini --del test.ini test_section
獲取
crudini --del test.ini test_section test_param
如果該標(biāo)量不在某一個(gè)section里面,則section用一個(gè)空字符表示:
crudini --del test.ini '' test_param
合并
將another.ini配置文件合并到test.ini中:
crudini --merge test.ini < another.ini
方法二 :ConfigParser模塊
說(shuō)明
ConfigParser 模塊為常用的操作ini文件的模塊,但是存在一些缺陷,無(wú)法識(shí)別section的大小寫(xiě),無(wú)法讀取文件注釋,這樣修帶有注釋的配置文件時(shí)就會(huì)存在問(wèn)題。
用法示例
示例文件test.ini
[test_section] test_param = test_value
讀取
import ConfigParser config = ConfigParser.ConfigParser() config.readfp(open('test.ini')) test_value = config.get("test_section","test_param")
寫(xiě)入
添加section
import ConfigParser config = ConfigParser.ConfigParser() # set a value of parameters config.add_section("test_section2") config.set("test_section2", "test_param2", "test_value2") config.set("test_section3", "test_param3", "test_value3") # write to file config.write(open('test.ini', "w"))
修改
import ConfigParser config = ConfigParser.ConfigParser() config.read('1.ini') config.set("test_section", "test_param3", "test_value3") config.write(open('test.ini', "r+"))
方法三:configobj模塊
說(shuō)明
正常的讀配置文件的方法是給ConfigObj一個(gè)文件名,然后通過(guò)字典來(lái)訪問(wèn)成員,子段來(lái)獲取value值,不會(huì)存在注釋無(wú)法讀取的缺陷
用法示例
示例文件test.ini
[test_section] test_param = test_value
讀取
from configobj import ConfigObj config = ConfigObj("test.ini",encoding='UTF8') # 讀配置文件 print config['test_section'] print config['test_section']['test_param ']
修改
from configobj import ConfigObj config = ConfigObj("test.ini",encoding='UTF8') config['test_section']['test_param '] = "test_value2" # 寫(xiě)入 config.write()
添加section
from configobj import ConfigObj config = ConfigObj("test.ini",encoding='UTF8') config['test_section2'] = {} config['test_section2']['test_param'] = "test_value" # 寫(xiě)入 config.write()
刪除
from configobj import ConfigObj config = ConfigObj("test.ini",encoding='UTF8') del config['test_section2']['test_param'] config.write()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
菜鳥(niǎo)使用python實(shí)現(xiàn)正則檢測(cè)密碼合法性
本文給大家分享了2則使用Python實(shí)現(xiàn)正則表達(dá)式檢測(cè)密碼合法性的代碼,由于是新手,所以方法比較笨,不過(guò)還是分享給小伙伴,希望對(duì)大家能夠有所幫助。2016-01-01安裝pycurl報(bào)錯(cuò)Could not run curl-config: &ap
這篇文章主要為大家介紹了安裝pycurl報(bào)錯(cuò)Could not run curl-config: 'curl-config'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12python BeautifulSoup庫(kù)的常用操作
Beautiful Soup 是一個(gè)可以從HTML或XML文件中提取數(shù)據(jù)的Python庫(kù),它能夠通過(guò)你喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查詢,修改文檔的方式,本文就來(lái)給大家簡(jiǎn)單介紹一下BeautifulSoup庫(kù)的常用操作,需要的朋友可以參考下2023-08-08使用wxpython實(shí)現(xiàn)的一個(gè)簡(jiǎn)單圖片瀏覽器實(shí)例
這篇文章主要介紹了使用wxpython實(shí)現(xiàn)的一個(gè)簡(jiǎn)單圖片瀏覽器實(shí)例,根據(jù)自己的特殊需求而寫(xiě),需要的朋友可以參考下2014-07-07解決pycharm中opencv-python導(dǎo)入cv2后無(wú)法自動(dòng)補(bǔ)全的問(wèn)題(不用作任何文件上的修改)
這篇文章主要介紹了解決pycharm中opencv-python導(dǎo)入cv2后無(wú)法自動(dòng)補(bǔ)全的問(wèn)題(不用作任何文件上的修改),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03