【python】python configparser模块
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模块的更多相关文章
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- python封装configparser模块获取conf.ini值
configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- python 配置文件 ConfigParser模块
ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...
随机推荐
- C++: Unresolved external symbol __imp__fscanf
VS2019中遇到错误error LNK2001: unresolved external symbol __imp__fscanf 解决办法:链接legacy_stdio_definitions.l ...
- Python编译出现错误SyntaxError: Non-ASCII character '\xe7' 时解决方法
转载个解决办法:https://blog.csdn.net/wangchao701123/article/details/57084244 转自https://blog.csdn.net/jim742 ...
- Python爬取链家二手房源信息
爬取链家网站二手房房源信息,第一次做,仅供参考,要用scrapy. import scrapy,pypinyin,requests import bs4 from ..items import L ...
- netcore mvc菜单,角色,权限
netcore mvc快速开发系统(菜单,角色,权限[精确到按钮])开源 AntMgr https://github.com/yuzd/AntMgr 基于netcore2.0 mvc 开发的 快速搭建 ...
- 微服务之Polly熔断策略
NET Core 微服务之Polly熔断策略 紧接着上一篇说,咱们继续介绍Polly这个类库 熔断策略(Circuit-breaker) 如果调用某个目标服务出现过多超时.异常等情况,可以采取一定时间 ...
- WebStorm 2018.2安装激活教程
1.下载解压,得到jetbrains webstorm 2018.2主程序,破解文件和中文语言包: 2.运行“WebStorm-2018.2.exe”开始安装,默认安装目录[C:\Program Fi ...
- amazeUI modal 模态框 关闭属性
$('#my-prompt').modal({ relatedTarget: this, closeViaDimmer: false, // 点击外部空白处不关闭弹窗 closeOnConfirm:f ...
- 前端入门系列之CSS
CSS (Cascading Style Sheets) 是用来样式化和排版你的网页的 —— 例如更改网页内容的字体.颜色.大小和间距,将内容分割成多列或者加入动画以及别的装饰型效果. CSS是什么 ...
- FCM实现手机推送,推送的方式,真机调试
FCM实现手机 1 下载Androidstudio 3.2 版本,之后新建一个项目 2 创建完项目之后查找文件AndroidMainifest.xml 3 访问网站注册一个自己的firebase的 ...
- 《Tsinghua os mooc》第15~16讲 处理机调度
第十五讲 处理机调度 进程调度时机 非抢占系统中,当前进程主动放弃CPU时发生调度,分为两种情况: 进程从运行状态切换到等待状态 进程被终结了 可抢占系统中,中断请求被服务例程响应完成时发生调度,也分 ...