最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python操作配置文件ini的三種方法講解

 更新時間:2019年02月22日 15:15:57   作者:energysober  
今天小編就為大家分享一篇關(guān)于Python操作配置文件ini的三種方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

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)文章

最新評論

吉水县| 广州市| 道真| 区。| 沙洋县| 拉萨市| 开原市| 奉化市| 防城港市| 余江县| 夹江县| 馆陶县| 格尔木市| 普格县| 罗定市| 西充县| 蓝山县| 德令哈市| 山东| 佳木斯市| 烟台市| 渭源县| 定结县| 保康县| 即墨市| 黄大仙区| 淳安县| 内黄县| 舟曲县| 鄂尔多斯市| 海兴县| 兴化市| 基隆市| 石泉县| 宜兰县| 泽州县| 南京市| 云梦县| 旌德县| 永吉县| 沁阳市|