国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-16 01:20:48
字體:
來源:轉載
供稿:網友

python 操作配置文件ini的三種方法

方法一:crudini 命令

說明

crudini命令是Linux下的一個操作配置文件的命令工具

用法

crudini --set [--existing] config_file section [param] [value] # 修改配置文件內容crudini --get [--format=sh|ini] config_file [section] [param] # 獲取配置文件內容crudini --del [--existing] config_file section [param]     # 刪除配置文件內容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

如果該標量不在某一個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 ConfigParserconfig = ConfigParser.ConfigParser()config.readfp(open('test.ini'))test_value = config.get("test_section","test_param")

寫入

添加section

import ConfigParserconfig = ConfigParser.ConfigParser()# set a value of parametersconfig.add_section("test_section2")config.set("test_section2", "test_param2", "test_value2")config.set("test_section3", "test_param3", "test_value3")# write to fileconfig.write(open('test.ini', "w"))

修改

import ConfigParserconfig = 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 ']             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼玛县| 孝义市| 于都县| 峡江县| 江都市| 达拉特旗| 文水县| 平武县| 广丰县| 自治县| 徐闻县| 丹凤县| 大姚县| 马龙县| 突泉县| 镇平县| 翼城县| 班玛县| 岳池县| 建宁县| 辽宁省| 雅江县| 玛纳斯县| 平远县| 尉犁县| 夏邑县| 陇西县| 准格尔旗| 都江堰市| 时尚| 滨海县| 通州区| 八宿县| 新密市| 盐城市| 苗栗市| 上犹县| 荥阳市| 蒙山县| 呼图壁县| 同仁县|