最近写程序要用到配置文件,那么配置文件的解析就很重要了,下文转自chinaunix

一、ConfigParser简介

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

1: [db]
2: db_host = 127.0.0.1
3: db_port = 22
4: db_user = root
5: db_pass = rootroot
6:  
7: [concurrent]
8: thread = 10
9: processor = 20

中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。

二、ConfigParser 初始工作

使用ConfigParser 首选需要初始化实例,并读取配置文件:

1: cf = ConfigParser.ConfigParser() 
2: cf.read("配置文件名")
 

三、ConfigParser 常用方法

1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:

1: s = cf.sections() 
2: print 'section:', s

将输出(以下将均以简介中配置文件为例):

1: section: ['db', 'concurrent']

2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:

1: o = cf.options("db")
2: print 'options:', o

将输出:

1: options: ['db_host', 'db_port', 'db_user', 'db_pass']

3. 获取指定section 的配置信息

1: v = cf.items("db")

2: print 'db:', v

将输出:

1: db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]

4. 按照类型读取指定section 的option 信息

同样的还有getfloat、getboolean。

1: #可以按照类型读取出来

2: db_host = cf.get("db", "db_host")

3: db_port = cf.getint("db", "db_port")

4: db_user = cf.get("db", "db_user")

5: db_pass = cf.get("db", "db_pass")

6:

7: # 返回的是整型的

8: threads = cf.getint("concurrent", "thread")

9: processors = cf.getint("concurrent", "processor")

10:

11: print "db_host:", db_host

12: print "db_port:", db_port

13: print "db_user:", db_user

14: print "db_pass:", db_pass

15: print "thread:", threads

16: print "processor:", processors

将输出:

1: db_host: 127.0.0.1

2: db_port: 22

3: db_user: root

4: db_pass: rootroot

5: thread: 10

6: processor: 20

5. 设置某个option 的值。(记得最后要写回)

1: cf.set("db", "db_pass", "zhaowei")

2: cf.write(open("test.conf", "w"))

6.添加一个section。(同样要写回)

1: cf.add_section('liuqing')

2: cf.set('liuqing', 'int', '15')

3: cf.set('liuqing', 'bool', 'true')

4: cf.set('liuqing', 'float', '3.1415')

5: cf.set('liuqing', 'baz', 'fun')

6: cf.set('liuqing', 'bar', 'Python')

7: cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')

8: cf.write(open("test.conf", "w"))

7. 移除section 或者option 。(只要进行了修改就要写回的哦)

1: cf.remove_option('liuqing','int')

2: cf.remove_section('liuqing')

3: cf.write(open("test.conf", "w"))

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. from ConfigParser import ConfigParser
  3. CONFIGFILE="f.txt"
  4. config=ConfigParser()
  5. config.read(CONFIGFILE)
  6. print config.get('messages','greeting')
  7. radius=input(config.get('messages','questions')+' ')
  8. print config.get('messages','result')
  9. print config.getfloat('numbers','pi')*radius**2
  10. s=config.sections()
  11. print'section: ',s
  12. o=config.options('messages')
  13. print'messages option: ',o
  14. v=config.items("messages")
  15. print'message de xinxi: ',v
  16. config.add_section('liuyang1')
  17. config.set('liuyang1','int','15')
  18. config.set('liuyang'1,'hhhh','hello world')
  19. config.write(open("f.txt","w"))
  20. print config.get('liuyang1','int')
  21. print config.get('liuyang1','hhhh')
  22. #!/usr/bin/env python
  23. import ConfigParser
  24. import sys
  25. config=ConfigParser.ConfigParser()
  26. config.add_section("book1")
  27. config.set("book1","title","hello world")
  28. config.set("book1","aut","log")
  29. config.write(open("f.txt","w"))

python ConfigParse模块(转)的更多相关文章

  1. python configparse模块&xml模块

    configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...

  2. python模块: hashlib模块, configparse模块, logging模块,collections模块

    一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...

  3. Python模块-configparse模块

    configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...

  4. Python进阶-XVV hashlib模块、configparse模块、logging模块

    1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...

  5. python学习-58 configparse模块

    configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...

  6. 【转】Python3 configparse模块(配置)

    [转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...

  7. 14 ConfigParse模块

    1.ConfigParse模块的基本概念 此模块用于生成和修改常见配置文档. ConfigParser 是用来读取配置文件的包. 配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  8. configParse模块

    一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...

  9. python常用模块-1

    一.认识模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文 ...

随机推荐

  1. 国产服务器离线安装gm

    离线安装过程: 1.安装JPEGlib cd /opt/ ls tar -zxvf jpegsrc.v9b.tar.gz cd jpeg-9b/ ./configure make make insta ...

  2. Python的subprocess模块(二)

    原文:http://blog.chinaunix.net/uid-26000296-id-4461522.html 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. ...

  3. 002-java语言基础

    一.安装卸载 卸载:控制面板 安装:下载对应版本 注意1.安装路径→尽量不要有空格和汉字 注意2.安装之后,jre可以不用安装,jdk中含有 二.环境变量 环境变量:理解,一些快捷路径.方便快速查找应 ...

  4. 测试:safenet提供的CheckKey函数 内存泄漏。具体来说是句柄.

    unsigned char vendor_code[] = "7XSQT4jxlSkDJhwqpxxfLwbuxgrYw93OMy+K5sc5pyfTa7HQo1ikLyg7FDuEpgUK ...

  5. python3 用requests 保存网页以及BeautifulSoup保存图片,并且在本地可以正常显示文章的内容和图片

    用requests 模块做了个简单的爬虫小程序,将博客的一篇文章以及图片保存到本地,文章格式存为'.html'.当文章保存到本地后,图片的连接可能是目标站点的绝对或者相对路径,所以要是想在本地也显示图 ...

  6. Python 函数的使用小结

    函数的好处:提高代码复用性,简化代码,代码可扩展. 函数只有调用的时候才会被执行. 1.参数: 形参&实参:位置参数,属于必填参数:默认值参数,为非必填参数,没有传值时使用默认值:关键字参数: ...

  7. python全栈开发从入门到放弃之socket并发编程之IO模型

    一 IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...

  8. windows安装redis, php5.5

    全套安装包地址 http://download.csdn.net/detail/whellote/9572797   解压 redis-2.2.5-win32-win64, 将里面的内容拷贝到j:/r ...

  9. Leetcode 357

    没用过Leetcode刷题,只能按照自己的想法随便写写了 思路:1.第一位数有9种(除了0)可能,第二位数有9种(除了第一位)可能,第三位数有8种(除了前两位)可能,以此类推...9*8*7*...( ...

  10. org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.my.service.ProductService] for bean with name 'productService' defi报错解决方法

    原 javaweb项目报错org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [XXX] ...