熟悉使用ConfigParser库读写配置文件
Python的配置文件
配置文件
setting.ini文件是一个纯文本
[DataBase1]
username = admin
passwors = root
[DataBase2]
hostname = 127.0.0.1
port = 3306
这是一般配置文件的常见的格式,[]里面叫做Section部分的名称,整个[]以及下面的每一项(每一个key=value,叫做Option)都是一个Section区域
Python读取配置文件
#引入依赖库,Python2.x引入ConfigParser Python3.x引入configparser
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader
要实现的功能
# -*- coding:utf-8 -*-
"""
自定义配置文件读取库
调用ConfigParser,只实现常用功能,作为学习ConfigParser和自用库
"""
#引入依赖库,Python2.x引入ConfigParser Python3.x引入configparser
import json
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader
#定义类和函数
class ConfigChannel:
"""建立配置读取类"""
def __init__(self, configFile):
"""建立ConfigChannel对象"""
try:
self.configFile = configFile
self.channel = cReader.SafeConfigParser()
self.channel.read(open(configFile))
except Exception as reason:
raise
def getOption(self, Section, Option):
"""获取某区域的某一项配置"""
try:
return self.channel.getfloot(Section, Option)
except Exception as reason:
try:
return self.channel.getint(Section, Option)
except Exception as reason:
try:
return self.channel.getboolean(Section, Option)
except Exception as reason:
return self.channel.get(Section, Option)
def hasOption(self, Section, Option):
"""判断是否存在这个Option"""
try:
return self.channel.has_option(Section, Option)
except Exception as reason:
raise
def showOptions(self):
"""展示所有Options"""
try:
return self.channel.options()
except Exception as reason:
raise
def setOption(self, Section, Option, Value):
"""会写或增加配置某区域某一项配置"""
try:
self.channel.set(Section, Option, Value)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def rmvOption(self, Section, Option):
"""删除某一个配置项"""
try:
self.channel.remove_option(Section, Option)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def hasSection(self, Section):
"""判断是否存在这个Section"""
try:
return self.channel.has_section(Section)
except Exception as reason:
raise
def showOptions(self):
"""展示所有Sections"""
try:
return self.channel.sections()
except Exception as reason:
raise
def addSection(self, Section):
"""增加一个配置区域"""
try:
self.channel.add_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def rmvSection(self, Section):
"""删除一个配置区域"""
try:
self.channel.remove_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def getConfigFromString(self, string):
"""从字符串读取配置并写入"""
try:
_dictionary = json.loads(string)
return self.getConfigFromDictionary(_dictionary)
except Exception as reason:
pass
try:
configList = string.split(":")
if len(configList) != 3 or '' in configList:
raise Exception
Section = configList[0]
Option = configList[1]
Value = configList[2]
if self.hasSection(Section):
self.setOption(Section, Option, Value)
else:
self.addSection(Section)
self.setOption(Section, Option, Value)
except Exception as reason:
raise
def getConfigFromDictionary(self, dictionary):
"""从字典读取配置并写入"""
if not isinstance(dictionary, dict):
raise
try:
for key in dictionary:
if not isinstance(dictionary.get(key), dict):
continue
if not self.channel.hasSection(key):
self.addSection(key)
for subkey in dictionary[key]:
self.channel.setOption(key, subkey,
dictionary[key][subkey])
except Exception as reason:
raise
熟悉使用ConfigParser库读写配置文件的更多相关文章
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- python:利用configparser模块读写配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- configparser模块读写ini配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- ConfigParser 读写配置文件
一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...
- Python-通过configparser读写配置文件
Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...
- python利用ConfigParser读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section, section 下有option=value这样的键值 ...
随机推荐
- 【FastJSON】使用JSON.toJSONString()-解决FastJson中“$ref 循环引用”的问题
fastjson 是一个 不错的json格式化工具, 但是在使用时,如果 碰到统一地址对象引用,就会用$ref替代 . 怎么去掉ref呢, 解决方法如下: String mapStr = JSONOb ...
- iOS 多线程简单使用的具体解释
主线程 一个iOS程序执行后.默认会开启1条线程,称为"主线程"或"UI线程"(刷新UI界面最好在主线程中做.在子线程中可能会出现莫名其妙的BUG) 主线程的作 ...
- html5 返回当前地理位置的坐标点(经纬度)
BAIDU <!DOCTYPE html> <html> <body> <p id="demo">点击这个按钮,获得您的坐标:< ...
- Java编程思想学习笔记——枚举类型
前言 关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 正文 基本enum特性 调用enum的values()方法可以遍历enum实例,value ...
- 织梦漏洞可疑PHP文件/article文件夹
常见可疑文件夹: 1:article文件夹:最近很多织梦系统网站根目录被上传article文件件,里面有很多赌博静态违法html页面,可疑直接删除,此类违法信息大部分是由于您的网站存在dedecms安 ...
- shell脚本自动清理服务器日志、图片等信息
在做性能测试的时候,linux服务器时常会产生大量数据,如日志信息,图片信息,文件信息等,压测一段时间后,导致服务器磁盘空间暂满而崩溃,每天手动清理比较麻烦, 利用shell脚本自动清理,脚本如下 1 ...
- Python 常用类库
python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的libraries(modules)如下: 1 ...
- 扫盲 -- What's MOOC ?
FAQ 1. MOOC是什么? 2. xMOOC又是什么? 它与之前在中国大陆网络上风靡一时的国外大学"公开课"有什么区别?3. xMOOC什么时候, 怎样出现的? 4. 有哪些网 ...
- 架设SVN服务器
服务器端:windows 2003 + subversion 下载地址:http://subversion.tigris.org/servlets/ProjectDocumentList?folder ...
- 【GIS】ArcGIS JS 4.X
require(["esri/Map", "esri/views/SceneView", "esri/TileLayer/TdtMapLayer/Td ...