python configparser模块

 

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

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

configparser函数常用方法:

读取配置文件:

 1 read(filename) #读取配置文件,直接读取ini文件内容
2
3 sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']
4
5 options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']
6
7 items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
8
9 get(section, option) #获取section中option的值,返回为string类型
10 >>>>>获取指定的section下的option <class 'str'> 127.0.0.1
11
12 getint(section,option) 返回int类型
13 getfloat(section, option) 返回float类型
14 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', '3306'), ('user', 'root'), ('password', '123456')]
获取指定的section下的option <class 'str'> 127.0.0.1

综合使用方法:

import configparser
"""
读取配置文件信息
""" class ConfigParser(): config_dic = {}
@classmethod
def get_config(cls, sector, item):
value = None
try:
value = cls.config_dic[sector][item]
except KeyError:
cf = configparser.ConfigParser()
cf.read('settings.ini', encoding='utf8') #注意setting.ini配置文件的路径
value = cf.get(sector, item)
cls.config_dic = value
finally:
return value if __name__ == '__main__':
con = ConfigParser()
res = con.get_config('logging', 'level')
print(res)

Python读取ini配置文件的方式的更多相关文章

  1. python读取ini配置文件的示例代码(仅供参考)

    这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...

  2. Python读取ini配置文件(接口自动测试必备)

    前言 大家应该接触过.ini格式的配置文件.配置文件就是把一些配置相关信息提取出去来进行单独管理,如果以后有变动只需改配置文件,无需修改代码. 特别是后续做自动化的测试,代码和数据分享,进行管理.比如 ...

  3. python读取 ini 配置文件

    在详解python读取ini文件之前,我们先说明一个ini文件的组成: 一个ini文件是由多个section组成,每个section中以key=vlaue形式存储数据: 然后我们来使用python读取 ...

  4. Python读取ini配置文件

    db_config.ini [baseconf] host=127.0.0.1 port=3306 user=root password=root db_name=evaluting_sys [con ...

  5. Python读取ini配置文件封装方法

    读取配置文件 ----rw_ini.py from configparser import ConfigParser def read_config(config_file_path:str): &q ...

  6. python 读取ini 配置文件

    安装 pip install configparser 1 配置文件 config.ini: [MysqlDB]user=rootpasswd=123456sport=3306db_name=my_d ...

  7. Python 读取写入配置文件 —— ConfigParser

    Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...

  8. 转 python3 读取 ini配置文件

    在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...

  9. 读取ini配置文件 及 UI对象库

    读取ini配置文件 配置项 读取API 写入API 实战:UI 对象库 读取ini配置文件 配置项 在每个 ini 配置文件中,配置数据会被分组(比如下述配置文件中的"config" ...

随机推荐

  1. vue.js组件(component)

    简介: 组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面 ...

  2. Spring中的用到的设计模式大全

    spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...

  3. circRNA研究手册

    环状RNA(circRNA)研究技术手册.doc.pdf (转自:汉恒生物)

  4. lncRNA研究利器之"TANRIC"

    http://bioinformatics.mdanderson.org/main/Main_Page http://ibl.mdanderson.org/tanric/_design/basic/i ...

  5. React Native基础概念和基础认识

    学习地址:https://github.com/vczero/react-native-lesson 当我们初始化一个RN项目的时候主要的是index.ios.js文件和index.android.j ...

  6. 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列

    Problem G: Array C Time Limit: 1 Sec  Memory Limit: 128 MB Description Giving two integers  and  and ...

  7. Linux 常用的压缩命令有 gzip 和 zip

    Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...

  8. js获取url 参数

    window.getRequest = function (url) { var theRequest = new Object(); var indexOf = url.indexOf(" ...

  9. 新的请求方式 fetch和axios

    参考链接:https://www.javascriptcn.com/read-5840.html axios使用文档: https://www.kancloud.cn/yunye/axios/2348 ...

  10. WPF 元素的查找

    预设置元素名字 WPF有两种方式设置元素的Name <StackPanel x:Name="panel"> <Label Name="name1&quo ...