Python操作配置文件ini的三種方法講解
python 操作配置文件ini的三種方法
方法一:crudini 命令
說明
crudini命令是Linux下的一個操作配置文件的命令工具
用法
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)量不在某一個section里面,則section用一個空字符表示:
crudini --del test.ini '' test_param
合并
將another.ini配置文件合并到test.ini中:
crudini --merge test.ini < another.ini
方法二 :ConfigParser模塊
說明
ConfigParser 模塊為常用的操作ini文件的模塊,但是存在一些缺陷,無法識別section的大小寫,無法讀取文件注釋,這樣修帶有注釋的配置文件時就會存在問題。
用法示例
示例文件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模塊
說明
正常的讀配置文件的方法是給ConfigObj一個文件名,然后通過字典來訪問成員,子段來獲取value值,不會存在注釋無法讀取的缺陷
用法示例
示例文件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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)查詢實例
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之?dāng)?shù)據(jù)查詢實例,需要的朋友可以參考下2014-06-06
python實現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
這篇文章主要介紹了python實現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法,結(jié)合實例形式分析了Python針對Unicode編碼操作的相關(guān)技巧及編碼轉(zhuǎn)換中的常見問題解決方法,需要的朋友可以參考下2017-04-04
Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能,結(jié)合實例形式分析了Python基于itchat模塊控制電腦實現(xiàn)運行程序、截圖等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
Python實戰(zhàn)實現(xiàn)爬取天氣數(shù)據(jù)并完成可視化分析詳解
這篇文章主要和大家分享一個用Python實現(xiàn)的小功能:獲取天氣數(shù)據(jù),進(jìn)行可視化分析,帶你直觀了解天氣情況!感興趣的小伙伴可以學(xué)習(xí)一下2022-06-06
Django制作簡易注冊登錄系統(tǒng)的實現(xiàn)示例
本文介紹了如何使用Django搭建一個簡易的注冊登錄系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時鐘
pyglet是一個面向Python的跨平臺窗口、多媒體庫,它可以用于創(chuàng)建游戲和多媒體應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于如何一步一步教你用Python?pyglet仿制鴻蒙系統(tǒng)里的時鐘,需要的朋友可以參考下2024-03-03
Python在實時數(shù)據(jù)流處理中集成Flink與Kafka
隨著大數(shù)據(jù)和實時計算的興起,實時數(shù)據(jù)流處理變得越來越重要,Flink和Kafka是實時數(shù)據(jù)流處理領(lǐng)域的兩個關(guān)鍵技術(shù),下面我們就來看看如何使用Python將Flink和Kafka集成在一起吧2025-03-03
Python3 Tensorlfow:增加或者減小矩陣維度的實現(xiàn)
這篇文章主要介紹了Python3 Tensorlfow:增加或者減小矩陣維度的實現(xiàn),具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python unittest裝飾器實現(xiàn)原理及代碼
這篇文章主要介紹了Python unittest裝飾器實現(xiàn)原理及代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

