最近写程序要用到配置文件,那么配置文件的解析就很重要了,下文转自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. Linux下的内核模块机制

    2017-06-20 Linux的内核模块机制允许开发者动态的向内核添加功能,我们常见的文件系统.驱动程序等都可以通过模块的方式添加到内核而无需对内核重新编译,这在很大程度上减少了操作的复杂度.模块机 ...

  2. 转载:futex同步机制详解

    在编译2.6内核的时候,你会在编译选项中看到[*] Enable futex support这一项,上网查,有的资料会告诉你"不选这个内核不一定能正确的运行使用glibc的程序", ...

  3. (转)Unity3d的3种截图方法

    下面是我总结的.在u3d中的,三种截屏方法: 1.使用Application类下的CaptureScreenshot方法. void CaptureScreen() { Application.Cap ...

  4. 浅谈pipreqs组件(自动生成需要导入的模块信息)

    简介 pipreqs的作用 一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt. 如果一个项目使用时virtua ...

  5. 对比python的进程和线程:多线程是假的

    进程,是系统进行资源分配最小单位(拥有独立的内存单元).(python中多进程是真的) 线程,是操作系统最小的执行单位(共享内存资源),比进程还小.(python中多线程是假的,因为cpython解释 ...

  6. OCR技术浅探: 光学识别(3)

    经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择 在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积神经网络,构建了 ...

  7. NodeJS开发环境搭建

    原创作者: zero20121222 NodeJS开发环境搭建 1.安装NodeJS 1.编译环境 源代码编译器,通常 Unix/Linux平台都自带了C++的编译器(GCC/G++).如果没有,请通 ...

  8. Spark如何写入HBase/Redis/MySQL/Kafka

    一些概念 一个partition 对应一个task,一个task 必定存在于一个Executor,一个Executor 对应一个JVM. Partition 是一个可迭代数据集合 Task 本质是作用 ...

  9. C++ error C2064:

    error C2064: term does not evaluate to a function 错误原因,非函数,当成函数使用了

  10. web.xml中配置spring配置(application.xml)文件

    application.xml 一般放到WEB-INF下,当然,你也可以将它放到任意问题,但需要web.xml指向到该文件 1.application.xml配置 <?xml version=& ...