1.模块简介

  configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。

2.configparser函数常用方法

read(filename) # 读取配置文件,直接读取ini文件内容

sections() # 获取ini文件内所有的section,以列表形式返回

options(sections) # 获取指定sections下所有options ,以列表形式返回

items(sections) # 获取指定section下所有的键值对

get(section, option) # 获取section中option的值,返回为string类型

getint(section, option) # 获取section中option的值,返回为int类型

getfloat(section, option) # 获取section中option的值,返回为float类型

getboolean(section, option) # 获取section中option的值,返回为boolean类型

例如:

拥有配置文件:my_config.conf,内容如下:

[teacher]
name=whh
sex=女
class=python [student]
name=xxxx
sex=女
class=python
teacher=whh
age=17
res=True
weight=70.55
hobby=["1","2","3","4"] [mobile_phone]
os=android

代码如下:

import configparser

# 实例化
cp = configparser.ConfigParser() # 加载读取配置文件
cp.read("my_config.conf",encoding="utf-8") # 获取配置文件所有的section
sections = cp.sections()
print(sections) # 获取配置文件下的某一个section
section = cp.options("student")
print(section) # 获取某一个section下的所有键值对
items = cp.items("teacher")
print(items) # 列表 # 获取某一个section下的某一个options具体的值
get_str = cp.get("student","class") # 字符串类型
print(get_str) get_int = cp.getint("student","age") # 整数类型
print(get_int) get_float = cp.getfloat("student","weight") # 浮点数类型
print(get_float) get_boolean = cp.getboolean("student","res") # 布尔值类型
print(get_boolean) # 转成列表
hobby = cp.get("student","hobby")
print(eval(hobby)) 答案:
['teacher', 'student', 'mobile_phone']
['name', 'sex', 'class', 'teacher', 'age', 'res', 'weight', 'hobby']
[('name', 'whh'), ('sex', '女'), ('class', 'python')]
python
17
70.55
True
['1', '2', '3', '4']

(15)-Python3之--configparser模块的更多相关文章

  1. Python3之configparser模块

    1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...

  2. python3 之configparser 模块

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...

  3. Python3 中 configparser 模块解析配置的用法详解

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  4. Python3 中 configparser 模块用法

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  5. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  6. 【python3】configparser读取ini配置文件

    在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...

  7. Python3.x:ConfigParser模块的使用

    Python3.x:ConfigParser模块的使用 简介 ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节 ...

  8. Python3 logging模块&ConfigParser模块

    ''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...

  9. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

随机推荐

  1. C# HTTP1.0 1.1 2.0与HTTPS 、TCP/IP协议的UDP与TCP、 Socket介绍与WebSocket

    一.HTTP1.0 1.1 2.0和HTTPS 1.HTTP协议是什么? HTTP协议是超文本传输协议的缩写,英文是Hyper Text Transfer Protocol.它是从WEB服务器传输超文 ...

  2. MVC中Autofac的使用

    参考博文 https://www.cnblogs.com/liupeng/p/4806184.html https://blog.csdn.net/qq_37214567/article/detail ...

  3. 前后端分离项目获取后端跨控制器获取不到session

    最近做前后端分离项目(.net core web api  +vue)时,后台跨控制器不能获取到session.由于配置的是共享的session.本来以为是共享session出了问题,就在共享sess ...

  4. 什么是可变参数?如何创建不可变集合?Steam三类方法是什么?获取流方法特点?流中间方法特点?终结流方法特点?

    ==知识梳理== ==重难点梳理== ==今日目标== 1.能够了解什么是可变参数 2.能够了解如何去创建不可变集合 3.能够掌握Stream流的使用 ==知识点== 1.可变参数 2.Stream流 ...

  5. JPA 复杂查询 - Querydsl

     添加依赖 <!--query dsl --> <dependency> <groupId>com.querydsl</groupId> <art ...

  6. MongoDB用户权限操作语法及示例

    1.创建用户 1.1.语法格式: 1.1.1.格式及例子 >db.createUser( { user: "<name>", pwd: "<cle ...

  7. Redis学习笔记之数据库(一)

     说句实话,redis这个软件要学习的东西实在多,多到,看的多了就容易迷失,而且还记不住.个人觉得靠记忆去学习一个知识肯定是比较糟糕的,所以还是要带着理解的,最终变成自己的东西,那这个东西才是自己的. ...

  8. TurtleBot3 Waffle (tx2版华夫)(11)建图-karto建图

    1)[Remote PC] 启动roscore $ roscore 2)[TurBot3] 启动turbot3 $ roslaunch turbot3_bringup minimal.launch 3 ...

  9. css浅谈

    一 CSS文字属性: color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体*/ font-size : 9pt; /*文字大小*/ ...

  10. 设计模式之SOLID原则

    介绍 设计模式中的SOLID原则,分别是单一原则.开闭原则.里氏替换原则.接口隔离原则.依赖倒置原则.前辈们总结出来的,遵循五大原则可以使程序解决紧耦合,更加健壮. SRP 单一责任原则 OCP 开放 ...