非标准配置文件也是经常使用的,如何使用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. Using QueryRunner to insert ArrayList<Object[]>

    使用QueryRunner 结合c3p0进行数据库操作时候, 需求:list<bean>进行插入数据库中,但是QueryRunner 仅仅支持batch():批处理: Object[][] ...

  2. 这篇通俗实用的Vlookup函数教程,5分钟就可以包你一学就会

    如何利用Vlookup函数获取学号中的班级信息.换言之,咱们源数据中放着姓名性别学号班级等信息,而在另一张表格中一定有学号信息,但其他信息就未必有,这需要我们将缺失的信息自动同步过去.使用vlooku ...

  3. net core 依懒注入 中间件

    依懒注入 依懒 当一个类需要另一个类协作来完成工作的时候就产生了依赖.比如我们在AccountController这个控制器需要完成和用户相关的注册.登录 等事情.其中的登录我们由EF结合Idneti ...

  4. *CTF 2019 quicksort、babyshell、upxofcpp

    这次参加比赛总共出了三道,有两道队友都先交了,还是tcl,heap_master卡了差不多一天没解决....还是记录一下出的题目吧 quicksort 题目大体流程就是输入要输入的数字数量,然后输入数 ...

  5. 利用kibana插件对Elasticsearch查询

    利用kibana插件对Elasticsearch查询 Elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据. 查询分类: 基本查询:使用Elasticsear ...

  6. Stream闪亮登场

    Stream闪亮登场 一. Stream(流)是什么,干什么 Stream是一类用于替代对集合操作的工具类+Lambda式编程,他可以替代现有的遍历.过滤.求和.求最值.排序.转换等 二. Strea ...

  7. Python(Django)遇到的问题及解决方法

    问题一 因为已经有程序占用了Django的默认端口了,所以只要这么启动项目,81是使用的端口,然后访问即可http://127.0.0.1:81/ 解决: 问题二 TypeError: not eno ...

  8. 匿名函数 javascript

    匿名函数: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. Tensor类型

    Tensor类型 1.Tensor有不同的数据类型,每种类型又有CPU和GPU两种版本: 2.默认的tensor类型是FloatTensor,t.set_default_tensor_type可以修改 ...

  10. S7 Connection 通讯

    参考两个链接: http://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=74626&cid=1029 https://su ...