Python-ConfigParser模块简介
来源:互联网 发布:银行系统网络拓扑图 编辑:程序博客网 时间:2024/06/08 07:29
一、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”))
#!/usr/bin/env pythonfrom ConfigParser import ConfigParserCONFIGFILE="f.txt"config=ConfigParser()config.read(CONFIGFILE)print config.get('messages','greeting')radius=input(config.get('messages','questions')+' ')print config.get('messages','result')print config.getfloat('numbers','pi')*radius**2s=config.sections()print'section: ',so=config.options('messages')print'messages option: ',ov=config.items("messages")print'message de xinxi: ',vconfig.add_section('liuyang1')config.set('liuyang1','int','15')config.set('liuyang'1,'hhhh','hello world')config.write(open("f.txt","w"))print config.get('liuyang1','int')print config.get('liuyang1','hhhh')
- Python-ConfigParser模块简介
- python ConfigParser模块简介
- Python之配置文件解析模块ConfigParser简介
- PYTHON 配置文件模块 ConfigParser
- Python模块之: ConfigParser
- python 之ConfigParser模块
- Python ConfigParser模块
- Python Configparser模块
- python ConfigParser 模块学习
- python 的 ConfigParser 模块
- python模块之ConfigParser
- Python ConfigParser模块教程
- python-ConfigParser模块学习
- python ConfigParser模块
- python ConfigParser模块详解
- python 模块configparser
- Python模块之ConfigParser
- Python模块之ConfigParser
- Web Server 架构浅谈-Staged Event-Driven Achitecture(SEDA)
- HDOJ 题目1905 Pseudoprime numbers(快速幂)
- ScrollView can host only one direct child
- [ASP.NET MVC 大牛之路]03 - C#高级知识点概要(2) - 线程和并发
- SQL四种语言,DDL、DCL、TCL和DCL
- Python-ConfigParser模块简介
- 企业管理软件中的组织模型和权限模型
- HDU1162 Eddy's picture 最小生成树
- 第三章 栈和队列
- jsonCpp使用介绍和优化
- 移动支付SDK2.0应用小结
- Avoid hardcoding the debug mode; leaving it out allows debug
- 黑马程序员——Java基础---IO(二)(缓冲区、字节流、流操作规律)--第19天--第226-247集
- Implicit delcaration of function 'sysctlbyname'is invalid in c99 问题及解决办法