python类的init方法(Python操作配置文件ini的三种方法讲解)
类别:脚本大全 浏览量:2350
时间:2022-01-21 00:14:58 python类的init方法
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] # 合并 |
举例
添加
|
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() |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/energysober/article/details/80684200
您可能感兴趣
- python代码词频分析(python实现词法分析器)
- python获取企业微信信息(通过shell+python实现企业微信预警)
- python3知识点汇总(Python3几个常见问题的处理方法)
- python3和python区别(Python2与Python3的区别实例总结)
- pythonlist列表讲解(Python中将两个或多个list合成一个list的方法小结)
- 用python制作一个简单的小程序(一个可以套路别人的python小程序实例代码)
- python怎么设置matlab编程(实例详解Matlab 与 Python 的区别)
- python类定义(浅谈python新式类和旧式类区别)
- python可视化界面实现管理系统(python面向对象实现名片管理系统文件版)
- python ip代理池(Python 实现域名解析为ip的方法)
- python3.7.0使用方法(Python3.5模块的定义、导入、优化操作图文详解)
- python字典键对应的值(Python 互换字典的键值对实例)
- python转pdf教程(Python实现将HTML转成PDF的方法分析)
- python中统计文本中单词数的代码(Linux上使用Python统计每天的键盘输入次数)
- python将txt数据写入excel(Python将列表数据写入文件txt, csv,excel)
- python迭代函数(详解Python中的内建函数,可迭代对象,迭代器)
- 王伦狭隘,晁盖霸道,宋江奸诈骨头软,只有鲁智深才适合当寨主(王伦狭隘晁盖霸道)
- 他是梁山最早的头目,江湖人称 旱地忽律 ,宋江几乎将其遗忘(他是梁山最早的头目)
- 梁山创始人杜迁,为何不受宋江待见,只排名83位(梁山创始人杜迁)
- 法国面包(法国面包法棍)
- 微信(微信分身)
- 双十二(双十二和双十一哪个划算)
热门推荐
- css选择器知识汇总(在Less中写IE的css hack的方法介绍)
- SqlParameter用法
- css的4个基础选择器(详解css常用选择器)
- python如何使用multiprocess(Python multiprocessing多进程原理与应用示例)
- jquery判断是否为数组
- laravel增删改查接口(laravel-admin 实现给grid的列添加行数序号的方法)
- pandas获取对应数值的索引(对pandas通过索引提取dataframe的行方法详解)
- opencv人脸识别实战(Python opencv实现人眼/人脸识别以及实时打码处理)
- php怎么实现多线程(PHP实现的多进程控制demo示例)
- 阿里云服务器实例用户名(阿里云服务器新建用户具体方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9