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类型,还有相应的getboolean()和getfloat() 函数。
 
2.基本的写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
 
3.基本例子
test.conf

[sec_a]
    a_key1 = 20
    a_key2 = 10
      
    [sec_b]
    b_key1 = 121
    b_key2 = b_value2
    b_key3 = $r
    b_key4 = 127.0.0.1

parse_test_conf.py

import ConfigParser
      
    cf = ConfigParser.ConfigParser()
      
    #read config
    cf.read("test.conf")
      
    # return all section
    secs = cf.sections()
    print 'sections:', secs
      
    opts = cf.options("sec_a")
    print 'options:', opts
      
    kvs = cf.items("sec_a")
    print 'sec_a:', kvs
      
    #read by type
    str_val = cf.get("sec_a", "a_key1")
    int_val = cf.getint("sec_a", "a_key2")
      
    print "value for sec_a's a_key1:", str_val
    print "value for sec_a's a_key2:", int_val
      
    #write config
    #update value
    cf.set("sec_b", "b_key3", "new-$r")
    #set a new value
    cf.set("sec_b", "b_newkey", "new-value")
    #create a new section
    cf.add_section('a_new_section')
    cf.set('a_new_section', 'new_key', 'new_value')
      
    #write back to configure file
    cf.write(open("test.conf", "w"))

得到终端输出:
sections: ['sec_b', 'sec_a']
options: ['a_key1', 'a_key2']
sec_a: [('a_key1', "i'm value"), ('a_key2', '22')]
value for sec_a's a_key1: i'm value
value for sec_a's a_key2: 22

更新后的test.conf

[sec_b]
    b_newkey = new-value
    b_key4 = 127.0.0.1
    b_key1 = 121
    b_key2 = b_value2
    b_key3 = new-$r
      
    [sec_a]
    a_key1 = i'm value
    a_key2 = 22
      
    [a_new_section]
    new_key = new_value

4.Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
 
设定配置文件test2.conf

[portal]
    url = http://%(host)s:%(port)s/Portal
    host = localhost
    port = 8080

使用RawConfigParser:

import ConfigParser
     
    cf = ConfigParser.RawConfigParser()
     
    print "use RawConfigParser() read"
    cf.read("test2.conf")
    print cf.get("portal", "url")
     
    print "use RawConfigParser() write"
    cf.set("portal", "url2", "%(host)s:%(port)s")
    print cf.get("portal", "url2")

得到终端输出:
use RawConfigParser() read
http://%(host)s:%(port)s/Portal
use RawConfigParser() write
%(host)s:%(port)s

改用ConfigParser:

import ConfigParser
     
    cf = ConfigParser.ConfigParser()
     
    print "use ConfigParser() read"
    cf.read("test2.conf")
    print cf.get("portal", "url")
     
    print "use ConfigParser() write"
    cf.set("portal", "url2", "%(host)s:%(port)s")
    print cf.get("portal", "url2")

得到终端输出:
use ConfigParser() read
http://localhost:8080/Portal
use ConfigParser() write
localhost:8080

改用SafeConfigParser:

import ConfigParser
     
    cf = ConfigParser.SafeConfigParser()
     
    print "use SafeConfigParser() read"
    cf.read("test2.conf")
    print cf.get("portal", "url")
     
    print "use SateConfigParser() write"
    cf.set("portal", "url2", "%(host)s:%(port)s")
    print cf.get("portal", "url2")

得到终端输出(效果同ConfigParser):
use SafeConfigParser() read
http://localhost:8080/Portal
use SateConfigParser() write
localhost:8080

【转载】Python ConfigParser的使用的更多相关文章

  1. [转载] Python数据类型知识点全解

    [转载] Python数据类型知识点全解 1.字符串 字符串常用功能 name = 'derek' print(name.capitalize()) #首字母大写 Derek print(name.c ...

  2. [转载]Python 包管理工具

    [转载]Python 包管理工具 最近由于机缘巧合,使用各种方法安装了一些Python包,所以对Python的包管理开始感兴趣.在网上找到一篇很好的文章:https://blog.zengrong.n ...

  3. 转载--python模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  4. 转载:python基础之模块

    作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 模块,用一 ...

  5. [转载]Python 资源大全中文版

    [转载]Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python ...

  6. [转载]Python 元组、列表、字典、文件

    python的元组.列表.字典数据类型是很python(there python is a adjective)的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益 ...

  7. [转载]Python 资源大全

    原文链接:Python 资源大全 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex  ...

  8. [转载] python 计算字符串长度

    本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...

  9. python configparser模块

    来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...

随机推荐

  1. pairs 和 ipairs异同

    同:都是能遍历集合(表.数组) 异:ipairs 仅仅遍历值,按照索引升序遍历,索引中断停止遍历.即不能返回 nil,只能返回数字 0,如果遇到 nil 则退出.它只能遍历到集合中出现的第一个不是整数 ...

  2. 使用向量化的 if:ifelse

    进行分支计算的一个替代方法是 ifelse( ).这个函数接收一个逻辑向量作为判定条件,并且返回一个向量.对于逻辑判定条件内的每一个元素,若是 TRUE,则选择第 2个参数 yes 中所对应的元素:若 ...

  3. http cookie的domain使用

    问题描述 最近遇到了一个因cookie domain设置不正确导致公司自研的分布式session组件无法生效的问题. 公司自研的这套分布式session组件依赖于设置在cookie中的sessionI ...

  4. Rails-Treasure chest1 (自定义Model网址;多语言包; 时区设置, TimeZone类; 格式日期时间; 表单单选UI; 表单多选UI;Select2 Plugin)

    自定义Model网址: 随机值网址SecureRandom.base58 多语言包, 包括默认语言设置和user自设置. 时区设置, TimeZone类 ,增加user自选时区功能 格式日期时间: x ...

  5. CentOS下安装Python-pip

    1.安装epel-release软件包:自动配置yum的软件仓库,弥补centos内容更新有时比较滞后或是一些扩展的源没有. yum -y install epel-release 2.安装pytho ...

  6. Educational Codeforces Round 23C

    超级坑的水题!!!想了两天没一点思路,看了题解第一段话就做出来了 刚开始一直在想找到通项就是例如an*10^n+...+a0*10^0-an-...-a0>=s,然后从这个里面找到规律,结果走进 ...

  7. bzoj-4870-组合dp+矩阵幂

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 829  Solved: 446[Submit][Statu ...

  8. 【hive】cube和rollup函数

    cube 数据立方体(Data Cube),是多维模型的一个形象的说法.(关于多维模型这里不讲述,在数据仓库设计过程中还挺重要的,有兴趣自行查阅) 立方体其本身只有三维,但多维模型不仅限于三维模型,可 ...

  9. halcon之屌炸天的自标定(1)

      本次先对halcon的自标定做个整体介绍,了解屌炸天的自标定在实际应用中的应用与实现方法,具体的编程细节将在后续的文章中介绍. halcon提供了一种自标定的算子,它可以在不用标定板的情况下,标定 ...

  10. 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件.

    'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件. 今天中午新换了一个系统,重装了一下wampserver2.2.下午想导入一个数据库文件打开dos,输入MySQL -u root - ...