python(60):configparser 函数,配置文件
ConfigParser模块学习
介绍
ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用再程序中硬编码,可以是你的程序变得灵活起来。
注意:在python 3 中ConfigParser模块名已更名为configparser
函数
读取配置文件
- read(filename) 直接读取ini文件内容
- sections() 得到所有的section,并以列表的形式返回
- options(section) 得到该section的所有option
- items(section) 得到该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类型
写入配置文件
- add_section(section) 添加一个新的section
- has_section(section) 判断是否有section
- set( section, option, value) 对section中的option进行设置
- remove_setion(section)删除一个section
- remove_option(section, option)删除section中的option
- write(fileobject)将内容写入配置文件。
使用
注:传入的是字符串,不加引号;
配置文件config.ini如下:
[user]
username = tom
password = ***
email = test@host.com [book]
bookname = python
bookprice = 25
注意:也可以使用 “,”,替换 “=”
程序:
# -* - coding: UTF-8 -* -
import ConfigParser
import sys reload(sys)
sys.setdefaultencoding("utf-8") #生成config对象
conf = ConfigParser.ConfigParser()
#用config对象读取配置文件
conf.read("config.ini")
#以列表形式返回所有的section
sections = conf.sections()
print 'sections:', sections #sections: ['user', 'book']
#得到指定section的所有option
options = conf.options("user")
print 'options:', options #options: ['username', 'password', 'email']
#得到指定section的所有键值对
useritem = conf.items("user")
print 'user:', useritem #user: [('username', 'tom'), ('password', '***'), ('email', 'test@host.com')]
#指定section,option读取值
str_val = conf.get("book", "bookname")
int_val = conf.getint("book", "bookprice") print "value for book's bookname:", str_val #value for book's bookname: python
print "value for book's bookprice:", int_val #value for book's bookprice: 25
#写配置文件
#更新指定section,option的值
conf.set("book", "bookname", "python learning")
#写入指定section增加新option和值
conf.set("book", "bookpress", u"人民邮电出版社")
#增加新的section
conf.add_section('purchasecar')
conf.set('purchasecar', 'count', '')
#写回配置文件
conf.write(open("config.ini", "w"))
python(60):configparser 函数,配置文件的更多相关文章
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- python利用ConfigParser读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section, section 下有option=value这样的键值 ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- Python 中 configparser 配置文件的读写及封装,配置文件存放数据,方便修改
1. 将程序中不常变化的数据放在配置文件中,有什么好处? 将配置统一放在一起,进行统一管理,方便维护,方便修改 配置文件将存放测试数据比如: Excel文件名. 日志名. 用例执行的结果. 实际结果和 ...
- python中configparser模块记录
python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...
- Python中操作ini配置文件
这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
随机推荐
- PyCharm 和 IntelliJ IDEA的破解激活
本教程对jetbrains全系列可用,例如:IDEA.WebStorm.phpstorm.clion等 PyCharm激活: 方法一: server选项里边输入 http://elporfirio. ...
- Linux学习之常用网络通信命令与shell简单应用技巧(四)
(一)常用网络通信命令 (1)ping命令 (2)write命令 (3)wall命令 (4)ifconfig命令 (5)shutdown命令 (6)reboot命令 (二)shell简单应用技巧 (1 ...
- [漏洞分析]thinkphp 5.x全版本任意代码执行分析全记录
0x00 简介 2018年12月10日中午,thinkphp官方公众号发布了一个更新通知,包含了一个5.x系列所有版本存在被getshell的高风险漏洞. 吃完饭回来看到这个公告都傻眼了,整个tp5系 ...
- .net系统的MD5加密方法
/// <summary>/// .net系统的MD5加密方法/// </summary>/// <param name="strIN">< ...
- jQuery Ajax -附示例
jQuery其实就是一个JavaScript的类库,其将复杂的功能做了上层封装,使得开发者可以在其基础上写更少的代码实现更多的功能. jQuery 不是生产者,而是大自然搬运工. jQuery Aja ...
- 认证登录装饰器与form组件的使用
def auth(func): '''制作登录认证的装饰器''' def inner(request,*args,**kwargs): user_info=request.session.get(se ...
- rabbitmq使用(四)
In the previous tutorial we built a simple logging system. We were able to broadcast log messages to ...
- 数据结构笔记1_求第k个最大者
参考的文章有: http://www.cnblogs.com/CCBB/archive/2009/06/01/1493971.html http://www.cnblogs.com/zhangchao ...
- spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析
更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...
- bat遍历目录
方法一: for /r D:\要遍历的目录\ %%i (*.exe) do ( echo %%i ) 方法二: set DestPath=D:\你的目录\ rem 你的后缀 set Dest ...