Python参数解析器configparser简介

作者:味卜鲜码 时间:2021-04-22 02:23:31 

Python参数解析器configparser简介

1.configparser介绍

configparser是python自带的配置参数解析器。可以用于解析.config文件中的配置参数。ini文件中由sections(节点)-key-value组成

Python参数解析器configparser简介

2.安装:

pip install configparse

3.获取所有的section

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#获取所有的section
sections = cf.sections()
print(sections)
#输出:['CASE', 'USER']

4.获取指定section下的option

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#获取指定section下所有的option
options = cf.options("CASE")
print(options)
#输出:['caseid', 'casetitle', 'casemethod', 'caseexpect']

5.获取指定section的K-V

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码#获取指定section下的option和value,每一个option作为一个元祖[(),(),()]
alls = cf.items("CASE")
print(alls)
#输出:[('caseid', '[1,2,3]'), ('casetitle', '["正确登陆","密码错误"]'), ('casemethod', '["get","post","put"]'), ('caseexpect', '0000')]

6.获取指定value(1)

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#获取指定section下指定option的value
caseid = cf.get("CASE","caseid")
print(caseid)

7.获取指定value(2)

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#获取指定section下指定option的value
caseid = cf["CASE"]["caseid"]
print(caseid)
#输出:[1,2,3]

8.value数据类型

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#value数据类型
user = cf["USER"]["user"]
print(type(user))
#输出:<class 'str'>

9.value数据类型还原eval()

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#读取config,有中文注意编码
#value数据类型还原
user = cf["USER"]["user"]
print(type(user))#输出:<class 'str'>
user = eval(user)
print(type(user))#输出:<class 'list'>

10.封装

import configparser

class GetConfig():
   def get_config_data(self,file,section,option):
       cf = configparser.ConfigParser()
       cf.read(file, encoding="utf8")  # 读取config,有中文注意编码
       # 返回value
       return cf[section][option]

if __name__ == '__main__':
   values = GetConfig().get_config_data("case.config","USER","user")
   print(values)
   #输出:[{"username":"张三","password":"123456"},{"username":"李四"}]

来源:https://www.cnblogs.com/QAbujiaban/p/16995421.html

标签:Python,参数解析器,configparser
0
投稿

猜你喜欢

  • python 计算数组中每个数字出现多少次--“Bucket”桶的思想

    2023-06-28 19:37:55
  • Tensorflow2.1 MNIST图像分类实现思路分析

    2023-04-17 03:35:32
  • python实现多线程端口扫描

    2021-07-06 03:27:26
  • Python中关键字nonlocal和global的声明与解析

    2023-08-01 13:11:10
  • Python实现自动签到脚本功能

    2022-07-24 21:53:40
  • 关于Torch torchvision Python版本对应关系说明

    2021-06-17 09:13:52
  • 如何使用微信公众平台开发模式实现多客服

    2023-11-14 17:48:37
  • 攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)

    2023-09-05 19:29:06
  • python实现sm2和sm4国密(国家商用密码)算法的示例

    2021-11-17 08:02:13
  • 聊聊python中的循环遍历

    2022-06-17 23:44:43
  • Python文本相似性计算之编辑距离详解

    2022-04-28 12:14:23
  • python和c语言哪个更适合初学者

    2022-06-22 08:23:29
  • 用SQL语句生成带有小计合计的数据集脚本

    2009-01-06 11:33:00
  • 搭建 Selenium+Python开发环境详细步骤

    2022-10-12 17:34:02
  • 总结分析Python的5个硬核函数

    2022-04-12 11:05:42
  • MySQL高级查询方法之记录查询

    2010-06-20 14:48:00
  • 交互设计实用指南系列(12)—避免出错

    2010-04-12 13:02:00
  • sqlserver isnull在数据库查询中的应用

    2011-12-01 10:30:25
  • sqlserver数据库主键的生成方式小结(sqlserver,mysql)

    2012-08-21 10:25:45
  • python皮尔逊相关性数据分析分析及实例代码

    2021-03-12 13:23:34
  • asp之家 网络编程 m.aspxhome.com