1. ConfigParser 简介
    ConfigParser是用来操作配置文件的模块。
      说明:[**]为配置文件的section,基本格式为
      [section]
      key = value
    eg:
      [db]
      db_host = 127.0.0.1
      db_port = 22
      db_user = root
      db_pass = rootroot
      [concurrent]
      thread = 10
      processor = 20
  2. eg
    #-*- encoding:UTF-8 -*-
    import ConfigParser def main():
    #初始化
    cf = ConfigParser.ConfigParser()
    cf.read("test.conf") # 获得所有的section
    section = cf.sections()
    print 'section:', section #获取指定section下所有的option(key)
    keys = cf.options("db")
    print 'options:', keys #获取指定section的option的值
    value = cf.items("db")
    print 'db:', value #可以按照类型读取出来,同样的还有getfloat、getboolean
    db_host = cf.get("db", "db_host")
    db_port = cf.getint("db", "db_port")
    db_user = cf.get("db", "db_user")
    db_pass = cf.get("db", "db_pass")
    # 返回的是整型的
    threads = cf.getint("concurrent", "thread")
    processors = cf.getint("concurrent", "processor")
    print "db_host:", db_host
    print "db_port:", db_port
    print "db_user:", db_user
    print "db_pass:", db_pass
    print "thread:", threads
    print "processor:", processors #修改一个option(key)的值,(记得写回去)
    cf.set("db", "db_pass", "hanhong")
    cf.write(open("test.conf", "w")) #添加一个section。(写回去)
    cf.add_section('liuqing')
    cf.set('liuqing', 'int', '')
    cf.set('liuqing', 'bool', 'true')
    cf.set('liuqing', 'float', '3.1415')
    cf.set('liuqing', 'baz', 'fun')
    cf.set('liuqing', 'bar', 'Python')
    cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
    cf.write(open("test.conf", "w")) #移除section 或者option 。(只要进行了修改就要写回)
    cf.remove_option('liuqing','int')
    cf.remove_section('liuqing')
    cf.write(open("test.conf", "w")) if __name__ == "__main__":
    main()

    输出:

    section: ['db', 'concurrent']
    options: ['db_host', 'db_port', 'db_user', 'db_pass']
    db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]
    db_host: 127.0.0.1
    db_port: 22
    db_user: root
    db_pass: rootroot
    thread: 10
    processor: 20

python 之ConfigParser的更多相关文章

  1. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  2. python中confIgparser模块学习

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

  3. python之ConfigParser的使用。

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  4. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

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

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

  6. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

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

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

  8. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

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

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

随机推荐

  1. [Java] xms xmx XX:PermSize XX:MaxPermSize 参数意义解析

    今天在做jmeter压力测试时又出现以前经常出现的异常,如下图,长时间不弄这个的,又有点不知所措了,所以干脆再来总结一下问题: 以前写过两篇文章,对这个问题研究过,见下面连接: 连接1:http:// ...

  2. [Linux][Hadoop] 运行WordCount例子

    紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子.   参照博客进行运行:http://xiejiangl ...

  3. 解决"System.AccessViolationException”类型的未经处理的异常在 未知模块(IIS Worker Process 已停止工作)导致无法连接远程数据库的问题

    解决方法: 用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)

  4. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  5. 智能车学习(八)——菜单的实现

    一.代码分享 1.头文件 #ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu ...

  6. github 使用网址

    [Github教程]史上最全github使用方法:github入门到精通 http://blog.csdn.net/hcbbt/article/details/11651229 githup的使用 h ...

  7. Linux学习笔记(4)Linux常用命令之权限管理命令

    (1)chmod chmod命令用于改变文件或目录权限,英文原意为change the permissions mode of a file,所在路径为/bin/chmod,其语法格式为: chmod ...

  8. ontouchstart

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta cont ...

  9. 圆形图片CircleImageView

    github源码路径: https://github.com/hdodenhof/CircleImageView 第一步:将CircleImageView复制 第二步:将attrs.xml复制 第三步 ...

  10. 【hibernate criteria】hibernate中criteria的完整用法 转

    ---恢复内容开始--- 转自:http://www.360doc.com/content/090313/10/26262_2794855.html 1.Criteria Hibernate 设计了 ...