ConfigParser模块学习

介绍

ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用再程序中硬编码,可以是你的程序变得灵活起来。 
注意:在python 3 中ConfigParser模块名已更名为configparser

函数

  1. 读取配置文件

    • 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类型
  2. 写入配置文件

    • 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 函数,配置文件的更多相关文章

  1. 记一次用python 的ConfigParser读取配置文件编码报错

    记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...

  2. python利用ConfigParser读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 ...

  3. Python利用ConfigParser读取配置文件

    http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...

  4. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  5. Python 中 configparser 配置文件的读写及封装,配置文件存放数据,方便修改

    1. 将程序中不常变化的数据放在配置文件中,有什么好处? 将配置统一放在一起,进行统一管理,方便维护,方便修改 配置文件将存放测试数据比如: Excel文件名. 日志名. 用例执行的结果. 实际结果和 ...

  6. python中configparser模块记录

    python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...

  7. Python中操作ini配置文件

    这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...

  8. python中confIgparser模块学习

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

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

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

随机推荐

  1. hdu 2005 求第几天(水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2005 转载于:https://blog.csdn.net/tigerisland45/article/ ...

  2. python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)

    python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...

  3. Redis分布式锁实现方式(附有正解及错误示例)

    一.前言 本文内容主要来自博客:https://wudashan.com/2017/10/23/Redis-Distributed-Lock-Implement/,本文用于归纳总结及笔记用途,如有需要 ...

  4. 收缩自编码器(CAE)

    自编码器是一种很好的降维技术,它可以学习到数据中非常有用的信息.而收缩自编码器作为正则自编码器的一种,其非线性降维效果非常好,并且它的过程可以通过流形知识来解释. 基础知识 1.自编码器 自编码器是一 ...

  5. es6的解构赋值用途

    (1)交换变量的值 let x = 1; let y = 2; [x, y] = [y, x]; 上面代码交换变量x和y的值,这样的写法不仅简洁,而且易读,语义非常清晰. (2)从函数返回多个值 函数 ...

  6. php 抽象类 静态 单体设计模式

    php oop----抽象类 抽象类机制使得子类可共用基类的某些信息,具体细节会留给子类,典型用在这样情形中,抽象类并不定义全部的方法,部分方法的实现推迟到子类继承抽象类时.它是介于接口和具体类间的一 ...

  7. Automatic overvoltage protection

    In most cases the voltage that is induced in the coil can not exceed 6V, and it does not have risk t ...

  8. android:提升 ListView 的运行效率

    之所以说 ListView 这个控件很难用,就是因为它有很多的细节可以优化,其中运行效率 就是很重要的一点.目前我们 ListView 的运行效率是很低的,因为在 FruitAdapter 的 get ...

  9. 使用jetty工具包处理url参数成map

    引入工具包: <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-u ...

  10. 最课程学员启示录:这么PL的小姐姐你要不要

    最课程学员启示录:这么PL的小姐姐给你做……你要不要? 想撒呢,给你做程序媛你要不要? 一句话,先上图,而且必须是经得住考验的素颜无码高清大图身份照: 我觉得未来我们可以搞个校花评选,你们不反对的话, ...