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
您可能感兴趣
- vscode如何配置python环境(VSCode Python开发环境配置的详细步骤)
- jupyter如何编写python(windows系统中Python多版本与jupyter notebook使用虚拟环境的过程)
- python迭代列表解析(Python可迭代对象操作示例)
- python函数操作大全(Python的高阶函数用法实例分析)
- python图片模板匹配(python实现简单图片物体标注工具)
- python编写小程序实现密码验证(python实现扫描ip地址的小程序)
- python删除list的指定元素(Python list列表中删除多个重复元素操作示例)
- python详细讲解类方法的使用(浅谈python标准库--functools.partial)
- python中对象方法和顶级方法(Python3.5面向对象程序设计之类的继承和多态详解)
- python怎样读取mysql数据(使用Python将Mysql的查询数据导出到文件的方法)
- 用python图片转字符串(python实现图片转字符小工具)
- python怎样读取txt文件的数据内容(Python实现读取txt文件中的数据并绘制出图形操作示例)
- pythonshell入门教程(python获取交互式ssh shell的方法)
- python模块使用方法(详解python的argpare和click模块小结)
- python异常值处理(一篇文章带你弄懂Python异常传递和自定义异常)
- python成绩判断系统(Python根据成绩分析系统浅析)
- 节日我在岗|警景相融 平安相伴(节日我在岗警景相融)
- 战 疫 时刻 致敬每一位石化大学的 守护者(战疫时刻)
- 老弄堂里的市井味道(老弄堂里的市井味道)
- 小米音乐可绑定QQ音乐, QQ音乐 真的会消失在小米的设备上吗(小米音乐可绑定QQ音乐)
- 小米Watch S1评测 或许能成为小米冲击高端可穿戴设备的里程碑(小米WatchS1评测或许能成为小米冲击高端可穿戴设备的里程碑)
- 手机QQ与小米路由器在一起 明天揭晓,敬请期待(手机QQ与小米路由器在一起)
热门推荐
- css中浮动的方式有几种方式(浅谈css中浮动和清除浮动带来的影响)
- dedecms迁移data验证码不显示(dedecms织梦上传图片302Error错误的解决方法)
- python气温变化数据分析(基于python历史天气采集的分析)
- mysql8.0关键字段使用(MySQL 8.0新特性之隐藏字段的深入讲解)
- laravel框架怎么获取变量(解决laravel id非自增 模型取回为0 的问题)
- php 大文件切片(PHP大文件切割上传功能实例分析)
- aspx页面无任何代码 每个页面发布成dll
- 云服务器linux图形化(linux云服务器如何扩容?)
- mysql面试题及答案100题(几个MySQL高频面试题的解答)
- 自己搭建域名解析服务器(apache 二级域名解析实现方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9