python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析
非标准配置文件也是经常使用的,如何使用configparser来解析?
这要从configparser本身解析结构来说,configparser包含section和option,非标准配置文件只有option,那么可以人为先加上一个section最后再去掉section
思路是这样,那么就可以操作了,我们使用config.ini文件如下:
globalmd5 = functest
port = 9900
address = http://sdv.functest.com
具体转换和增删改查操作参看如下代码:
import configparser
import os filepath = os.path.join(os.getcwd(),'config.ini')
print(filepath)
sectionname = 'temp' #把普通配置文件转换为有section的文件
def trans2ini(path,sectionname):
with open(path,'r') as f:
f_temp = f.read()
with open(path,'w') as f:
f.write('[' + sectionname + ']\n' + f_temp) #转换为不带section的配置文件
def trans2normal(path):
with open(path,'r') as f:
f.readline()
f_temp = f.read()
with open(path,'w') as f:
f.write(f_temp) #查询操作
def select(filepath,configparser):
configparser.read(filepath)
for i in configparser.sections():
print('[' + i + ']')
for k,v in configparser.items(i):
print(k,'=',v) #修改操作
def update(fielpath,configparser,section,option,value):
configparser.read(filepath)
configparser.set(section,option,value)
with open(filepath,'w+') as f:
configparser.write(f) #删除option操作
def delete_option(filepath,configparser,section,option):
configparser.read(filepath)
configparser.remove_option(section,option)
with open(filepath,'w+') as f:
configparser.write(f) #删除section操作
def delete_section(filepath,configparser,section):
configparser.read(filepath)
configparser.remove_option(section)
with open(filepath,'w+') as f:
configparser.write(f) #增加操作
def insert(filepath,configparser,section,options,values):
configparser.read(filepath)
if section not in configparser.sections():
configparser.add_section(section)
for i in range(len(options)):
configparser.set(section,options[i],values[i])
with open(filepath,'w+') as f:
configparser.write(f) #转换为带section的ini文件
trans2ini(filepath,sectionname)
cp = configparser.ConfigParser()
print('查询原始文件:')
select(filepath,cp) print('修改port为8809:')
update(filepath,cp,sectionname,'port','')
select(filepath,cp) print('删除port:')
delete_option(filepath,cp,sectionname,'port')
select(filepath,cp) print('增加port:')
insert(filepath,cp,sectionname,['port'],[''])
select(filepath,cp) #操作完成后删除section
trans2normal(filepath)
以上仅作为参考,有更好思路请留言交流
python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析的更多相关文章
- Python之配置文件模块 ConfigParser
写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...
- python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射
目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...
- python基础-7.3模块 configparser logging subprocess os.system shutil
1. configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 继承至2版本 ConfigParser,实现了更多智能特征,实现更有可预见性,新 ...
- Python基础之常用模块(三)
1.configparser模块 该模块是用来对文件进行读写操作,适用于格式与Windows ini 文件类似的文件,可以包含一个或多个节(section),每个节可以有多个参数(键值对) 配置文件的 ...
- Python学习 :常用模块(三)----- 日志记录
常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...
- python函数和常用模块(三),Day5
递归 反射 os模块 sys模块 hashlib加密模块 正则表达式 反射 python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数 ...
- python之读取配置文件模块configparser(一)基本操作
configparser模块是读取类ini文件使用,其有固定的读取格式如下: [section1] option11 = value11 option12 = value12 .... [sectio ...
- python之读取配置文件模块configparser(二)参数详解
configparser.ConfigParser参数详解 从configparser的__ini__中可以看到有如下参数: def __init__(self, defaults=None, dic ...
- Python urllib和urllib2模块学习(三)
build_opener()详解: 1.urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能,要支持这些功能,必须使用build_opener()函数创建自定这句话的 ...
随机推荐
- android新窗口以及传值
1,新建一个activity,如Activity2,在清单文件AndroidManifest.xml 中 application节点中 增加一个新窗体: ................. </ ...
- javamail 发送、读取邮件
概述 1.邮件相关的标准 厂商所提供的 JavaMail 服务程序可以有选择地实现某些邮件协议,常见的邮件协议包括: SMTP(Simple Mail Transfer Protocol) :即简单邮 ...
- notes for lxf(三)
纯函数式编程是没有变量的,只要输入确定输出就确定 指高度抽象的编程范式 特点 函数本身可以作为参数传入 或者允许返回一个函数 Higher-order function 一个函数可以接收另一个函数作为 ...
- 组合 数论 莫比乌斯反演 hdu1695
题解:https://blog.csdn.net/lixuepeng_001/article/details/50577932 题意:给定范围1-b和1-d求(i,j)=k的数对的数量 #includ ...
- OO Unit 2 电梯调度
目录 OO Unit2 博客作业 基于度量来分析⾃己的程序结构 复杂度分析 架构分析 改进和重构 发现过的BUG 简化问题 多线程初探 OO Unit2 博客作业 基于度量来分析⾃己的程序结构 自认为 ...
- win10自带的防火墙Windows Defender
Windows Defender防火墙(别名:windows守卫者)是微软公司自主研发的一款基于windows自身保护的一款系统. Windows Defender可以对系统进行实时监控,对于Wind ...
- BZOJ 3622
直接算 $a_i>b_i$ 对数恰为 $k$ 的不好算 那么可以先算 $a_i>b_i$ 对数至少 $k$ 的 这个排序后随便dp一下就好 那么再除了一下 用 $f_i$ 表示 $a_i& ...
- Winform消息与并行的形象比喻
有一次我给同事讲述跨线程调用时使用了高速行驶的并行列车来比喻,感觉比较形象. 线程列车 多线程就像多个并行的列车,每个线程在各自的轨道上不断向前行驶.主界面所在的线程称为UI线程,也叫主线程,主线程依 ...
- ES6学习
一.ES6的特点 1.let(变量),const(常量) 2.在ES6中不能重复定义 3.块级作用域 普通作用域 if(true){ var test =1; } console.log(test); ...
- for循环:用turtle画一颗五角星
import turtle # 设置初始位置 turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() turtle.right(9 ...