背景:

在写接口自动化框架,配置数据库连接时,测试环境和UAT环境的连接信息不一致,这时可以将连接信息写到conf或者cfg配置文件中

python环境请自行准备。

python代码直接封装成类,方便其他模块的引入。

 from configparser import ConfigParser

 class DoConfig:
def __init__(self,filepath,encoding='utf-8'):
self.cf = ConfigParser()
self.cf.read(filepath,encoding) #获取所有的section
def get_sections(self):
return self.cf.sections() #获取某一section下的所有option
def get_option(self,section):
return self.cf.options(section) #获取section、option下的某一项值-str值
def get_strValue(self,section,option):
return self.cf.get(section,option) # 获取section、option下的某一项值-int值
def get_intValue(self, section, option):
return self.cf.getint(section, option) # 获取section、option下的某一项值-float值
def get_floatValue(self, section, option):
return self.cf.getfloat(section, option) # 获取section、option下的某一项值-bool值
def get_boolValue(self, section, option):
return self.cf.getboolean(section, option) def setdata(self,section,option,value):
return self.cf.set(section,option,value) if __name__ == '__main__':
cf = DoConfig('demo.conf')
res = cf.get_sections()
print(res)
res = cf.get_option('db')
print(res)
res = cf.get_strValue('db','db_name')
print(res)
res = cf.get_intValue('db','db_port')
print(res)
res = cf.get_floatValue('user_info','salary')
print(res)
res = cf.get_boolValue('db','is')
print(res) cf.setdata('db','db_port','')
res = cf.get_strValue('db', 'db_port')
print(res)

python中使用configparser库,实现配置文件的读取的更多相关文章

  1. python 中使用ConfigParser类修改配置文件

    配置文件的格式: [user] user_ip=127.0.0.1 user_name=testuser user_id=13 import ConfigParser conf = ConfigPar ...

  2. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  3. 【归纳】正则表达式及Python中的正则库

    正则表达式 正则表达式30分钟入门教程 runoob正则式教程 正则表达式练习题集(附答案) 元字符\b代表单词的分界处,在英文中指空格,标点符号或换行 例子:\bhi\b可以用来匹配hi这个单词,且 ...

  4. 利用Python中的mock库对Python代码进行模拟测试

    这篇文章主要介绍了利用Python中的mock库对Python代码进行模拟测试,mock库自从Python3.3依赖成为了Python的内置库,本文也等于介绍了该库的用法,需要的朋友可以参考下     ...

  5. Python中使用第三方库xlrd来写入Excel文件示例

    Python中使用第三方库xlrd来写入Excel文件示例 这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表 ...

  6. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  7. 【转】利用Python中的mock库对Python代码进行模拟测试

    出处 https://www.toptal.com/python/an-introduction-to-mocking-in-python http://www.oschina.net/transla ...

  8. 熟悉使用ConfigParser库读写配置文件

    Python的配置文件 配置文件 setting.ini文件是一个纯文本 [DataBase1] username = admin passwors = root [DataBase2] hostna ...

  9. python中的configparser类

    Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置),所以可以自己写一个函数,实现读取config配置. config文件的写法比较简单,[sect ...

随机推荐

  1. Java中Integer和int的异同

    public void Test1() { int a = 128; Integer b = 128; Integer c = 128; //Integer会自动拆箱成int,所以为ture Syst ...

  2. C和C指针小记(十五)-结构和联合

    1.结构 1.1 结构声明 在声明结构时,必须列出它包含的所有成员.这个列表包括每个成员的类型和名称. struct tag {member-list} variable-list; 例如 //A s ...

  3. nginx基本用法和HTTPS配置

    nginx作用讲解:1.反向代理:需要多个程序共享80端口的时候就需要用到反向代理,nginx是反向代理的一种实现方式.2.静态资源管理:一般使用nginx做反向代理的同时,应该把静态资源交由ngin ...

  4. Elasticsearch学习笔记(二)Search API 与 Query DSL

    一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以 ...

  5. 格式化输出&初始编码&运算符

    一:格式化输出 %     %d   %s %为占位符   S替换的内容的类型为字符型 d替换的内容为整型 若在格式化输出的时候需要正常用到% 则表示时用两个%%表示 如: name = input( ...

  6. java 软件安装

    myeclipse 2014 安装 https://www.cnblogs.com/plus301/articles/5633540.html

  7. 20190412 T-SQL语言一

    -- T-SQL ------------------------------------------------------ 例如1 什么是注释符 单行注释select *from kc /*sel ...

  8. 编写自定义django-admin命令

    Django为项目中每一个应用下的management/commands目录中名字没有以下划线开始的Python模块都注册了一个manage.py命令,我们可以利用这点来自定制一个命令(比如运行该命令 ...

  9. java框架之SpringBoot(11)-缓存抽象及整合Redis

    Spring缓存抽象 介绍 Spring 从 3.1 版本开始定义了 org.springframework.cache.Cache 和 org.springframework.cache.Cache ...

  10. Lua C/C++互相调用

    先来说下大致脚本引擎框架,此次采用如下,即运行C++代码启动程序,然后加载Lua脚本执行! 1.基础 Lua脚本中只能调用 int (*lua_CFunction) (lua_State *L) 这种 ...