一、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之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模块应用

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

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

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

  5. python 的ConfigParser模块

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

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

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

  7. python中configparser模块

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

  8. python 之ConfigParser

    ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...

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

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

随机推荐

  1. LeetCode:数据库技术【175-178】

    LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...

  2. docker学习笔记2--对镜像/容器的命令操作

    Docker启动一个Centos镜像 我们下载完成一个Centos镜像之后,开始启动 docker run -d -i -t <imageID> /bin/bash 这样就能启动一个一直停 ...

  3. RHEL 5 安装gcc

    rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...

  4. 【LeetCode】【动态规划】Edit Distance

    描述 Given two words word1 and word2, find the minimum number of operations required to convert word1  ...

  5. 【leetcode刷题笔记】Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  6. ubuntu: lightdm 登录root超级管理员方法

    ubuntu 12.04 lts 默认是不允许root登录的, 在登录窗口只能看到普通用户和访客登录. 以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后, 修改系统配置文件需要切换到超级 ...

  7. Qt5.4.1移植到arm——Linuxfb篇

    Qt5与Qt4对比有很大的改变,其最大的特性在于模块化,并且很明显的是不再见到Qt4用到的qws,Qt5新增了QPA系统,基于QPA使得Qt5移 植到一个新平台非常简单而又具有极强的底层扩展能力:同时 ...

  8. MODBUS协议 一种问答方式的通信协议

    源:MODBUS协议 一种问答方式的通信协议 ModBus通信系统协议

  9. 关于pycharm中的requirements.txt文件

    作用:记录所有所依赖的第三方模块,方便迁移到不同的环境中后,防止缺少模块,或因为所依赖的第三方模块版本不同引起不必要的问题 生成命令:pip freeze > requirements.txt ...

  10. 20145230《Java程序设计》第3周学习总结

    20145230 <Java程序设计> 第3周学习总结 教材学习内容总结 String s=new String();第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定 ...