非标准配置文件也是经常使用的,如何使用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(三)高级使用---非标准配置文件解析的更多相关文章

  1. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  2. 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 ...

  3. python基础-7.3模块 configparser logging subprocess os.system shutil

    1. configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 继承至2版本 ConfigParser,实现了更多智能特征,实现更有可预见性,新 ...

  4. Python基础之常用模块(三)

    1.configparser模块 该模块是用来对文件进行读写操作,适用于格式与Windows ini 文件类似的文件,可以包含一个或多个节(section),每个节可以有多个参数(键值对) 配置文件的 ...

  5. Python学习 :常用模块(三)----- 日志记录

    常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...

  6. python函数和常用模块(三),Day5

    递归 反射 os模块 sys模块 hashlib加密模块 正则表达式 反射 python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数 ...

  7. python之读取配置文件模块configparser(一)基本操作

    configparser模块是读取类ini文件使用,其有固定的读取格式如下: [section1] option11 = value11 option12 = value12 .... [sectio ...

  8. python之读取配置文件模块configparser(二)参数详解

    configparser.ConfigParser参数详解 从configparser的__ini__中可以看到有如下参数: def __init__(self, defaults=None, dic ...

  9. Python urllib和urllib2模块学习(三)

    build_opener()详解: 1.urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能,要支持这些功能,必须使用build_opener()函数创建自定这句话的 ...

随机推荐

  1. Unity 5.x动态加载光照信息(所有坑已踩)

    能搜到这的应该是被新的烘焙系统坑了少时间,4.x到5.x美术必须重新烘焙,关于美术的没什么说的,只有---重新烘焙! 新的烘焙系统,为了兼容5.x的多场景编辑功能,将烘焙信息从mesh全部挪到了一个中 ...

  2. unity一个按钮实现开和关

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ButtonCl ...

  3. UOJ#424. 【集训队作业2018】count 多项式,FFT,矩阵

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ424.html 题解 主席太神仙了! 首先我们把题意转化成:对所有挺好序列建 笛卡尔树,有多少笛卡尔树互不 ...

  4. idea 版本控制 忽略要提交的文件

  5. mysql 分库分表转

    分表是分散数据库压力的好方法. 分表,最直白的意思,就是将一个表结构分为多个表,然后,可以再同一个库里,也可以放到不同的库. 当然,首先要知道什么情况下,才需要分表.个人觉得单表记录条数达到百万到千万 ...

  6. PLS:利用PLS(两个主成分的贡献率就可达100%)提高测试集辛烷值含量预测准确度并《测试集辛烷值含量预测结果对比》—Jason niu

    load spectra; temp = randperm(size(NIR, 1)); P_train = NIR(temp(1:50),:); T_train = octane(temp(1:50 ...

  7. 用python做一个搜索引擎(Pylucene)

    什么是搜索引擎? 搜索引擎是“对网络信息资源进行搜集整理并提供信息查询服务的系统,包括信息搜集.信息整理和用户查询三部分”.如图1是搜索引擎的一般结构,信息搜集模块从网络采集信息到网络信息库之中(一般 ...

  8. [SCOI2015]小凸玩矩阵

    Description: 给你一个n*m的网格,每个格子有一个数字,每行每列只能选一个数字,问所选数字中第k大的数字的最小值是多少 Hint: \(n \le 250\) Solution: 显然是二 ...

  9. vijos搭建踩坑

    nodejs我用的8.x版本,可以工作. 和制作组交谈之后他们说最好榨汁机和主机不要在同一系统下. vj4/vj4/handler/base.py的第343行 从 super(Connection, ...

  10. Hive+Sqoop+Mysql整合

    Hive+Sqoop+Mysql整合 在本文中,LZ随意想到了一个场景: 车,道路,监控,摄像头 即当一辆车在道路上面行驶的时候,道路上面的监控点里面的摄像头就会对车进行数据采集. 我们对采集的数据进 ...