背景:

在写接口自动化框架,配置数据库连接时,测试环境和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. VS code -python 使用笔记本

    1--使用虚拟环境 |----setting->search: python.venv->设置  venv path (你创建的虚拟环境文件夹所在路径,此处我的是 - 目录下) |---- ...

  2. Linux命令 sed

    一. 以行为单位进行操作. d:删除 $ nl passwd | sed '2,5d'  # 删除第2~5行 $ nl passwd | sed '2d'  # 删除第2行 $ nl passwd | ...

  3. .NET Core开发日志——OData

    简述 OData,即Open Data Protocol,是由微软在2007年推出的一款开放协议,旨在通过简单.标准的方式创建和使用查询式及交互式RESTful API. 类库 在.NET Core中 ...

  4. Codeforces 483 - A/B/C/D/E - (Done)

    题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned ...

  5. 内购审核被拒-[environment-sandbox]

    内购审核被拒-[environment-sandbox] 先把新创建的项目提交审核通过后再提交主包 注意,新增加的内购项目要截图告诉苹果

  6. nio例子

    跟传统io相比,nio会支持更大的并发量 nio去除了传统io的连接阻塞,和读写阻塞 服务器端 客户端 nio模型 传统io nio

  7. Django中CBV源码解析

    使用 关于FBV和CBV的使用在之前有提到,点击穿越. 准备 首先在视图中创建一个类并继承 django.views.View 类,在类中可定义各种请求方式对应执行的函数(函数名为请求方式名称小写). ...

  8. vue -about

    j基于webpack4 搭建vue 环境:https://juejin.im/post/5bc30d5fe51d450ea1328877

  9. java 和 c++ 实现的各种基础数据结构和算法

    https://github.com/phishman3579/java-algorithms-implementation https://github.com/xorz57/forest

  10. OAuth2认证和授权:ResourceOwnerPassword认证

    ResourceOwnerPassword在 ClientCredentials认证上新增了用户名和密码 但通过RequestPasswordTokenAsync获取不到refresh_token,不 ...