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

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

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

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

最新評(píng)論

江口县| 吴旗县| 手机| 南康市| 合山市| 广宁县| 沈阳市| 澳门| 苗栗县| 蒙阴县| 灌云县| 延吉市| 安吉县| 互助| 天水市| 读书| 剑川县| 阳信县| 新宾| 长沙市| 镇安县| 鹤山市| 南江县| 鄂伦春自治旗| 吴川市| 河津市| 汝州市| 乃东县| 乌拉特前旗| 汤原县| 南阳市| 紫阳县| 资溪县| 仪陇县| 万全县| 芦溪县| 蓬莱市| 思茅市| 旅游| 灵武市| 定日县|