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的大小寫,無(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")
寫入
添加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"
# 寫入
config.write()
添加section
from configobj import ConfigObj
config = ConfigObj("test.ini",encoding='UTF8')
config['test_section2'] = {}
config['test_section2']['test_param'] = "test_value"
# 寫入
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)文章
Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)查詢實(shí)例
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)查詢實(shí)例,需要的朋友可以參考下2014-06-06
python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法,結(jié)合實(shí)例形式分析了Python針對(duì)Unicode編碼操作的相關(guān)技巧及編碼轉(zhuǎn)換中的常見問(wèn)題解決方法,需要的朋友可以參考下2017-04-04
Python使用itchat模塊實(shí)現(xiàn)簡(jiǎn)單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實(shí)現(xiàn)簡(jiǎn)單的微信控制電腦功能,結(jié)合實(shí)例形式分析了Python基于itchat模塊控制電腦實(shí)現(xiàn)運(yùn)行程序、截圖等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
Python實(shí)戰(zhàn)實(shí)現(xiàn)爬取天氣數(shù)據(jù)并完成可視化分析詳解
這篇文章主要和大家分享一個(gè)用Python實(shí)現(xiàn)的小功能:獲取天氣數(shù)據(jù),進(jìn)行可視化分析,帶你直觀了解天氣情況!感興趣的小伙伴可以學(xué)習(xí)一下2022-06-06
Django制作簡(jiǎn)易注冊(cè)登錄系統(tǒng)的實(shí)現(xiàn)示例
本文介紹了如何使用Django搭建一個(gè)簡(jiǎn)易的注冊(cè)登錄系統(tǒng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘
pyglet是一個(gè)面向Python的跨平臺(tái)窗口、多媒體庫(kù),它可以用于創(chuàng)建游戲和多媒體應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于如何一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘,需要的朋友可以參考下2024-03-03
Python在實(shí)時(shí)數(shù)據(jù)流處理中集成Flink與Kafka
隨著大數(shù)據(jù)和實(shí)時(shí)計(jì)算的興起,實(shí)時(shí)數(shù)據(jù)流處理變得越來(lái)越重要,Flink和Kafka是實(shí)時(shí)數(shù)據(jù)流處理領(lǐng)域的兩個(gè)關(guān)鍵技術(shù),下面我們就來(lái)看看如何使用Python將Flink和Kafka集成在一起吧2025-03-03
Python3 Tensorlfow:增加或者減小矩陣維度的實(shí)現(xiàn)
這篇文章主要介紹了Python3 Tensorlfow:增加或者減小矩陣維度的實(shí)現(xiàn),具有好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python unittest裝飾器實(shí)現(xiàn)原理及代碼
這篇文章主要介紹了Python unittest裝飾器實(shí)現(xiàn)原理及代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

