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 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  2. 聊聊RPC原理二

    之前写了一篇关于RPC的文章,浏览量十分感人:),但是感觉文章写得有些粗,觉得很多细节没有讲出来,这次把里边的细节再次补充和说明. 这次主要说的内容分为: 1. RPC的主要结构图. 2.分析结构图的 ...

  3. MySQL 5.7基于GTID复制的常见问题和修复步骤(二)

    [问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when r ...

  4. Linux /etc/profile文件详解

    Linux /etc/profile文件详解   转载地址:http://linux.chinaitlab.com/administer/820910.html linux /etc/profile文 ...

  5. Lucene、ES好文章

    1.lucene4.5源码分析系列:lucene概述 http://blog.csdn.net/liweisnake/article/details/10348969 http://www.cnblo ...

  6. 仙剑奇侠传 游戏 开发 教程 Xianjian qixia development Game development tutorial

    仙剑奇侠传 开发  游戏 开发 教程 Xianjian qixia development Game development tutorial 作者:韩梦飞沙 Author:han_meng_fei_ ...

  7. bzoj 3143 随机游走

    题意: 给一个简单无向图,一个人从1号节点开始随机游走(即以相同概率走向与它相邻的点),走到n便停止,问每条边期望走的步数. 首先求出每个点期望走到的次数,每条边自然是从它的两个端点走来. /**** ...

  8. WPF(C#)与MATLAB混合编程

    WPF(C#)与MATLAB混合编程 WPF可以为开发者提供便捷地构建用户交互界面的解决方法,而matlab则在科学计算方面有着无与伦比的优势,因此在一些需要将科学算法转换为应用软件的项目中,需要应用 ...

  9. register form

    <code class="language-html"><div class="width100 marT15 content_news_list&qu ...

  10. How determine the RC time constant in PWM DAC low-pass filter?

    how determine the RC time constant in PWM digital to analog low-pass filter? I 'm looking for the be ...