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

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]             # 合并
 
 
  • 1
  • 2
  • 3
  • 4

举例

添加

 crudini --set test.ini test_section test_param test_value
 
 
  • 1

更新

 crudini --set [--existing]  test.ini test_section test_param test_value
 
 
  • 1

删除
删除param:

 crudini --del test.ini test_section test_param
 
 
  • 1

删除section:

 crudini --del test.ini test_section
 
 
  • 1

获取

 crudini --del test.ini test_section test_param
 
 
  • 1

如果该标量不在某一个section里面,则section用一个空字符表示:

 crudini --del test.ini '' test_param
 
 
  • 1

合并
将another.ini配置文件合并到test.ini中:

 crudini --merge test.ini < another.ini
 
 
  • 1

方法二 :ConfigParser模块

说明

ConfigParser 模块为常用的操作ini文件的模块,但是存在一些缺陷,无法识别section的大小写,无法读取文件注释,这样修带有注释的配置文件时就会存在问题。

用法示例

示例文件test.ini

[test_section]
test_param = test_value


 
 
  • 1
  • 2
  • 3
  • 4

读取

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open('test.ini'))
test_value = config.get("test_section","test_param")
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

写入
添加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"))
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

修改

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('1.ini')
config.set("test_section", "test_param3", "test_value3")

config.write(open('test.ini', "r+")) 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

方法三:configobj模块

说明

正常的读配置文件的方法是给ConfigObj一个文件名,然后通过字典来访问成员,子段来获取value值,不会存在注释无法读取的缺陷

用法示例

示例文件test.ini

[test_section]
test_param = test_value
 
 
  • 1
  • 2

读取

from configobj import ConfigObj  

config = ConfigObj("test.ini",encoding='UTF8')  

# 读配置文件  
print config['test_section']  
print config['test_section']['test_param ']  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改

from configobj import ConfigObj  

config = ConfigObj("test.ini",encoding='UTF8')  
config['test_section']['test_param '] = "test_value2"  
# 写入
config.write()  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

添加section

from configobj import ConfigObj  

config = ConfigObj("test.ini",encoding='UTF8')  
config['test_section2'] = {}  
config['test_section2']['test_param'] = "test_value"  
# 写入
config.write()  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

删除

from configobj import ConfigObj  

config = ConfigObj("test.ini",encoding='UTF8')  
del config['test_section2']['test_param']  
config.write()  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                            </div>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值