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. python按位操作以及进制转换

    a = raw_input() b = raw_input() c1 = int(str(a), 2)#2进制转化为10进制 c2 = int(str(b), 2) c = c1 ^ c2#按位异或 ...

  2. prim algorithm

    function re=biaoji(j,biao) %判断j点是否已被标记 l=length(biao); for i=1:l if j==biao(i) re=1; return; end end ...

  3. C#动态实体集的反序列化(动态JSON反序列化)

    一.使用场景 我们在将 JSON 反序列化实体集的时候,如果字段是固定的,那么我们序列化非常简单,对应字段写的实体集就可以了.比如下面这种: { "data":[ { " ...

  4. Asp.net Core使用Quartz.net

    1.介绍:Quartz.Net主要是用来做一些周期性的工作,或者定时工作.比如每天凌晨2点执行某个方法或者调用某个接口. Quartz项目地址:https://github.com/quartz-sc ...

  5. Dovecot邮件服务器的正确安装方法

    Dovecot邮件服务器的正确安装方法 apt remove dovecot-coredpkg -P dovecot-core sudo apt install dovecot-imapd dovec ...

  6. matplotlib学习日记(一)------图表组成元素

      1.使用函数绘制matplotlib的图表组成元素 (1)函数plot---变量的变化趋势 import matplotlib.pyplot as plt import numpy as np x ...

  7. [leetcode349]Intersection of Two Arrays

    设计的很烂的一道题 List<Integer> res = new ArrayList<>(); // int l1 = nums1.length; // int l2 = n ...

  8. execute,executeQuery,executeUpdate的区别是什么?

    a.Statement的execute(String query)方法用来执行任意的SQL查询,如果查询的结果是一个ResultSet,这个方法就返回true.如果结果不是ResultSet,比如in ...

  9. json 与 ajax

    json类似与js中的对象,但是json中不能有方法,json相当于python中的字典,但是json中的键值如果是字符串的话,需要加上双引号:ajax是一个前后台配合的技术,它可以让js发送http ...

  10. 大数据可视化呈现工具LightningChart的用法

    LightningChart (LightningChart Ultimate) 软件开发工具包是微软VisualStudio 的一个插件,专攻大数据可视化呈现问题,用于WPF(WindowsPres ...