Python参数解析器configparser简介

目录
  • 1.configparser介绍
  • 2.安装:
  • 3.获取所有的section
  • 4.获取指定section下的option
  • 5.获取指定section的K-V
  • 6.获取指定value(1)
  • 7.获取指定value(2)
  • 8.value数据类型
  • 9.value数据类型还原eval()
  • 10.封装

1.configparser介绍

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

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":"李四"}]

到此这篇关于Python参数解析器configparser的文章就介绍到这了,更多相关Python参数解析器内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 详解Python读取配置文件模块ConfigParser

    1,ConfigParser模块简介 假设有如下配置文件,需要在Pyhton程序中读取 $ cat config.ini [db] db_port = 3306 db_user = root db_host = 127.0.0.1 db_pass = xgmtest [SectionOne] Status: Single Name: Derek Value: Yes Age: 30 Single: True [SectionTwo] FavoriteColor = Green [SectionT

  • Python使用ConfigParser模块操作配置文件的方法

    本文实例讲述了Python使用ConfigParser模块操作配置文件的方法.分享给大家供大家参考,具体如下: 一.简介 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 二.配置文件格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecr

  • Python自动化测试ConfigParser模块读写配置文件

    Python自动化测试ConfigParser模块读写配置文件 ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 配置文件   test.conf    [section1] name = tank age = 28 [section2] ip = 192.168.1.1 port = 8080 Python代码 #

  • python中parser.add_argument()用法实例(命令行选项、参数和子命令解析器)

    目录 一.argparse介绍 二.argparse使用——代码示例 1.创建一个解析器——创建 ArgumentParser() 对象 2.添加参数——调用 add_argument() 方法添加参数 3.解析参数——使用 parse_args() 解析添加的参数 四.python args parse_args() 报错解决 1.error: the following arguments are required: xxx 五.其他问题汇总(评论小伙伴问的) 1.下划线_和横线-的区别 2

  • Python使用自带的ConfigParser模块读写ini配置文件

    在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦.Python自带有读取配置文件的模块ConfigParser,使用起来非常方便. ini文件 ini配置文件格式: 读取配置文件: import ConfigParser conf = ConfigParser.ConfigParser() conf.read('dbconf.ini') # 文件路径 name = conf.get("section1", "name&quo

  • Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该section的所有option -items(section) 得到该section的所有键值对 -get(section,option) 得到section中option的值,返回为string类型 -getint(section,option) 得到section中option的值,返回为int类型,

  • Python参数解析器configparser简介

    目录 1.configparser介绍 2.安装: 3.获取所有的section 4.获取指定section下的option 5.获取指定section的K-V 6.获取指定value(1) 7.获取指定value(2) 8.value数据类型 9.value数据类型还原eval() 10.封装 1.configparser介绍 configparser是python自带的配置参数解析器.可以用于解析.config文件中的配置参数.ini文件中由sections(节点)-key-value组成

  • Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】

    本文实例讲述了Python HTML解析器BeautifulSoup用法.分享给大家供大家参考,具体如下: BeautifulSoup简介 我们知道,Python拥有出色的内置HTML解析器模块--HTMLParser,然而还有一个功能更为强大的HTML或XML解析工具--BeautifulSoup(美味的汤),它是一个第三方库.简单来说,BeautifulSoup最主要的功能是从网页抓取数据.本文我们来感受一下BeautifulSoup的优雅而强大的功能吧! BeautifulSoup安装 B

  • Spring自定义参数解析器代码实例

    这篇文章主要介绍了Spring自定义参数解析器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 结合redis编写User自定义参数解析器UserArgumentResolver import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation

  • Python网页解析器使用实例详解

    python 网页解析器 1.常见的python网页解析工具有:re正则匹配.python自带的html.parser模块.第三方库BeautifulSoup(重点学习)以及lxm库. 2.常见网页解析器分类 (1)模糊匹配 :re正则表达式即为字符串式的模糊匹配模式: (2)结构化解析: BeatufiulSoup.html.parser与lxml,他们都以DOM树结构为标准,进行标签结构信息的提取. 3.DOM树解释:即文档对象模型(Document Object Model),其树形标签结

  • python 网页解析器掌握第三方 lxml 扩展库与 xpath 的使用方法

    今天说的则是使用另外一种扩展库 lxml 来对网页完成解析.同样的,lxml 库能完成对 html.xml 格式的文件解析,并且能够用来解析大型的文档.解析速度也是相对比较快的. 要掌握 lxml 的使用,就需要掌握掌握 xpath 的使用方法,因为 lxml 扩展库就是基于 xpath 的,所以这一章的重点主要还是对 xpath 语法使用的说明. 1.导入 lxml 扩展库.并创建对象 # -*- coding: UTF-8 -*- # 从 lxml 导入 etree from lxml im

  • 使用自定义参数解析器同一个参数支持多种Content-Type

    目录 一堆废话 探究Springmvc参数解析器工作流程 不想看废话的可以直接进结果 补充 一堆废话 事出有因, 原先上线的接口现在被要求用Java重写,按照原暴露出去的文档然后毫无疑问的,按照Java的惯例, 一定是@RequestBody然后去接收application/json;charset=utf-8,然后一通参数接收处理逻辑. 结果测试都通过了,上线的时候,刚把原接口切到新接口上,日志就狂飙 application/x-www-form-urlencoded:charset=utf-

  • SpringBoot项目实用功能之实现自定义参数解析器

    核心点 1.实现接口 org.springframework.web.method.support.HandlerMethodArgumentResolver supportsParameter 方法根据当前方法参数决定是否需要应用当前这个参数解析器 resolveArgument 执行具体的解析过程 2.将自实现的参数解析器类 添加到Spring容器中 3.实现 org.springframework.web.servlet.config.annotation.WebMvcConfigurer

  • 详解如何在SpringBoot中自定义参数解析器

    目录 前言 1.自定义参数解析器 2.PrincipalMethodArgumentResolver 3.RequestParamMapMethodArgumentResolver 4.小结 前言 在一个 Web 请求中,参数我们无非就是放在地址栏或者请求体中,个别请求可能放在请求头中. 放在地址栏中,我们可以通过如下方式获取参数: String javaboy = request.getParameter("name "); 放在请求体中,如果是 key/value 形式,我们可以通

  • Python配置文件解析模块ConfigParser使用实例

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key-value 的配置内容. 复制代码 代码如下: [db]  db_host = 127.0.0.1  db_port = 22  db_user = root  db_pass = rootroot    [concurrent]  thread = 10  processor = 20 中括号"

  • Spring boot中自定义Json参数解析器的方法

    一.介绍 用过springMVC/spring boot的都清楚,在controller层接受参数,常用的都是两种接受方式,如下 /** * 请求路径 http://127.0.0.1:8080/test 提交类型为application/json * 测试参数{"sid":1,"stuName":"里斯"} * @param str */ @RequestMapping(value = "/test",method = Re

随机推荐