ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

注意:在python 3 中ConfigParser模块名已更名为configparser

configparser函数常用方法:

读取配置文件:

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

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

 options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']

 items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]

 get(section, option) #获取section中option的值,返回为string类型
>>>>>获取指定的section下的option <class 'str'> 127.0.0.1 getint(section,option) 返回int类型
getfloat(section, option) 返回float类型
getboolean(section,option) 返回boolen类型

举例如下:

配置文件ini如下:

[logging]
level = 20
path =
server = [mysql]
host=127.0.0.1
port=3306
user=root
password=123456

注意,也可以使用:替换=

代码如下:

 import configparser
from until.file_system import get_init_path conf = configparser.ConfigParser()
file_path = get_init_path()
print('file_path :',file_path)
conf.read(file_path) sections = conf.sections()
print('获取配置文件所有的section', sections) options = conf.options('mysql')
print('获取指定section下所有option', options) items = conf.items('mysql')
print('获取指定section下所有的键值对', items) value = conf.get('mysql', 'host')
print('获取指定的section下的option', type(value), value)

运行结果如下:

file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
获取配置文件所有的section ['logging', 'mysql']
获取指定section下所有option ['host', 'port', 'user', 'password']
获取指定section下所有的键值对 [('host', '127.0.0.1'), ('port', ''), ('user', 'root'), ('password', '')]
获取指定的section下的option <class 'str'> 127.0.0.1

【python】python configparser模块的更多相关文章

  1. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  2. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  3. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  4. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  5. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  6. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  7. python封装configparser模块获取conf.ini值

    configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...

  8. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  9. (转)python的ConfigParser模块

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

  10. python 配置文件 ConfigParser模块

    ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...

随机推荐

  1. 123467123456#1#-----com.twoapp.DaDiShuGame01--前拼后广--现实打地鼠游戏jiemei

    com.twoapp.DaDiShuGame01--前拼后广--现实打地鼠游戏jiemei

  2. 123457123457#1#-----com.threeapp.ErTongHuaXue01----儿童滑雪大冒险

    123456123456#0#-----com.threeapp.ErTongHuaXue01----儿童滑雪大冒险

  3. SAP S/4 HANA与SAP Business Suite/R3(ECC)的区别

    转自:https://blog.csdn.net/zhongguomao/article/details/53515203 去年SAP推出了新一代商务套件SAP S/4 HANA,无疑是ERP行业创新 ...

  4. python数据分析数据标准化及离散化详解

    python数据分析数据标准化及离散化详解 本文为大家分享了python数据分析数据标准化及离散化的具体内容,供大家参考,具体内容如下 标准化 1.离差标准化 是对原始数据的线性变换,使结果映射到[0 ...

  5. Python中列表,元组,字典,集合的区别

    参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...

  6. 【Leetcode_easy】720. Longest Word in Dictionary

    problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...

  7. mybatis传入map任意表增删改查,分页过滤字段

    <!--根据实体参数查询 --> <select id="selectBaseList" resultType="java.util.HashMap&q ...

  8. pip3 install pyinstaller 报错了的处理方法

    http://www.pyinstaller.org/downloads.html 下载压缩包 解压到本地后,在目录处cmd 执行命令 python setup.py install 然后执行pip ...

  9. git merge仓

    git merge --no-ff branch 合并指定代码 如果有冲突 git mergetool   可视化解决冲突, qa! 全退出 如果修复失败 git checkout branch -f ...

  10. IntelliJ IDEA 简单设置

    1.在idea中添加try/catch的快捷键 ctrl+alt+t 2.主题修改 选择菜单栏“File--settings--apperance--theme”,主题选择Darcula: 3.代码字 ...