Python操作配置文件ini的三种方法讲解

yipeiwu_com6年前Python基础

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 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() 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

python通过paramiko复制远程文件及文件目录到本地

最近写运维自动化平台,需要用python写很多的小功能模块。 这里就分享一个用Python的paramiko来实现功能的一段代码: 复制远程服务器上的文件及文件夹到本地目录。 解释一下什...

python 处理数字,把大于上限的数字置零实现方法

如下所示: # coding=utf-8 # 用来处理数字,大于上限的数字置零 f = open("/home/chuwei/桌面/trainA/loss/d_losses.tx...

python机器学习实战之最近邻kNN分类器

K近邻法是有监督学习方法,原理很简单,假设我们有一堆分好类的样本数据,分好类表示每个样本都一个对应的已知类标签,当来一个测试样本要我们判断它的类别是, 就分别计算到每个样本的距...

numpy 进行数组拼接,分别在行和列上合并的实例

numpy 进行数组拼接,分别在行和列上合并的实例

在进行数据分析的时候,会把把一些具有多个特征的样本数据进行拼接合并吗,放在一起分析,预测.... 下面是用numpy中的函数进行数组的拼接。 (1)方法一。np.vstack() v 表...

Python3中常用的处理时间和实现定时任务的方法的介绍

无论哪种编程语言,时间肯定都是非常重要的部分,今天来看一下python如何来处理时间和python定时任务,注意咯:本篇所讲是python3版本的实现,在python2版本中的实现略有不...