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. SQL HAVING 子句使用

    HAVING 对由sum或其它集合函数运算结果的输出进行限制. 比如,我们可能只希望看到Store_Information数据表中销售总额超过1500美圆的商店的信息,这时我们就需要使用HAVING从 ...

  2. C++中的智能指针

    一.动态内存管理 通常我们创建动态内存的时候,需要自己管理好内存,也就是说,new出来的对象一定要注意释放掉.下面通过例子可以看到这个问题所在: struct BBE{ int X; int Y; v ...

  3. BZOJ.5417.[NOI2018]你的名字(后缀自动机 线段树合并)

    LOJ 洛谷 BZOJ 考虑\(l=1,r=|S|\)的情况: 对\(S\)串建SAM,\(T\)在上面匹配,可以得到每个位置\(i\)的后缀的最长匹配长度\(mx[i]\). 因为要去重,对\(T\ ...

  4. Python3基础之异常结构

    自定义异常类 class ShortInputException(Exception): def __init__(self, length, atleast): Exception.__init__ ...

  5. replace方法,替代敏感字符

    var val = $("#id").val(); var reg =/垃圾|你大爷|你妹的/g; if(val){   // false  : isNaN-- 0-- undef ...

  6. Monte Carlo计算Pi,python实现

    Monte Carlo import random import matplotlib.pyplot as plt import numpy as np 6 # 函数模拟点的随机掉落,并分为两组 de ...

  7. Python 将图片转化为 HTML 页面

    最近在 GitHub 看到一个挺有意思的 Python 程序(img2html: Convert a image to HTML). 它能将图片的每个像素用文字代替,最后生成一个HTML文档,在浏览器 ...

  8. C# 匿名类型如何使用

  9. python图片和分形树

    链接: 这10个Python项目很有趣! Python 绘制分形图(曼德勃罗集.分形树叶.科赫曲线.分形龙.谢尔宾斯基三角等)附代码 使用Python生成树形图案 神奇的代码:用 Python 生成分 ...

  10. Java知识回顾 (5)数组、日期与时间, StringBuffer和StringBuilder

    一.数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. dataType[] arrayRefVar; // 首选的方法 或 dataType arra ...