熟悉使用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这样的键值 ...
随机推荐
- Getting Started with Google Guava.pdf
- 【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌
<ignore_js_op> 软件名称:神行者(破解)软件版本:v1.3.0001授权类别:免费测试机型:大可乐手机 下载链接: http://pan.baidu.com/s/1qWwSM ...
- spring mvc 框架URL接收中文参数的乱码解决方案
后台可能就会出现乱码,具体解决方案如下: 一. 配置tomcat目录下的service.xml文件 tomcat7/conf/server.xml 给该行代码加上 URIEncoding=" ...
- MarkDown技巧:两种方式实现页内跳转
MarkDown技巧:两种方式实现页内跳转 本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/JohnT ...
- vue-router 2 跳转失败原因
axios.post('/internal/user/login_from_mobile ',{ mobile: this.logPrefix+this.formInline1.mobile, pas ...
- 【12-06】A股主要指数的市盈率(PE)估值高度
全指材料(SH000987) - 2018-12-06日,当前值:12.043,平均值:30.37,中位数:26.0097,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 中证煤 ...
- nginx 配置虚拟主机的三种方法
nginx,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管 ...
- [Scikit-learn] 1.4 Support Vector Machines - Linear Classification
Outline: 作为一种典型的应用升维的方法,内容比较多,自带体系,以李航的书为主,分篇学习. 函数间隔和几何间隔 最大间隔 凸最优化问题 凸二次规划问题 线性支持向量机和软间隔最大化 添加的约束很 ...
- Linux关闭Tomcat为什么要用Kill,而不是shutdown.sh
Linux关闭Tomcat为什么要用Kill,而不是shutdown.sh >>>>>>>>>>>>>>>&g ...
- GreenPlum数据库安装
第一章 文档概述 本文描述适用于Greenplum4.0以上版本的安装操作.所涉及到的操作系统相关参数调整,主要针对Redhat Linux操作系统. 第二章 安装介质 操作系统:Cent ...