ConfigParser模块学习: 用python解析配置文件
来源:互联网 发布:知乎 分级基金 编辑:程序博客网 时间:2024/06/07 05:04
在程序中使用配置文件来灵活的配置一些参数是一件很常 见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做 一些介绍。
ConfigParser解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项, 比如:
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
假设上面的配置文件的名字为test.conf。里面包含两个section,一个是db, 另一个是concurrent, db里面还包含有4项,concurrent里面有两项。这里来做做解析:
#-*- encoding: gb2312 -*-
- import ConfigParser
- import string, os, sys
- cf = ConfigParser.ConfigParser()
- cf.read("test.conf")
- # 返回所有的section
- s = cf.sections()
- print 'section:', s
- o = cf.options("db")
- print 'options:', o
- v = cf.items("db")
- print 'db:', v
- print '-'*60
- #可以按照类型读取出来
- 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
- #修改一个值,再写回去
- cf.set("db", "db_pass", "zhaowei")
- cf.write(open("test.conf", "w"))
- ConfigParser模块学习: 用python解析配置文件
- python模块之ConfigParser:解析配置文件模块
- python学习笔记3-解析配置文件ConfigParser模块
- python模块之ConfigParser: 用python解析配置文件
- python模块之ConfigParser: 用python解析配置文件
- Python模块:配置文件解析器configparser
- Python之配置文件解析模块ConfigParser简介
- Python 解析配置文件 (ConfigParser)模块
- 【Python】配置文件解析模块ConfigParser使用实例
- 解析配置文件ConfigParser模块
- PYTHON 配置文件模块 ConfigParser
- Python 配置文件 ConfigParser 模块
- [Python]ConfigParser解析配置文件
- Python用ConfigParser解析INI配置文件
- 使用ConfigParser模块解析配置文件
- python-ConfigParser模块【读写配置文件】
- 【ConfigParser模块】Python处理配置文件
- python-ConfigParser模块【读写配置文件】
- Python图书概览 -- 分类指引电子书 swords
- ubuntu上的ssh和crontab
- Oracle Golden Gate 系列十四 -- 监控 GG 状态 说明
- C#简单实现office转pdf、pdf转图片
- hashlib模块学习:hash算法库
- ConfigParser模块学习: 用python解析配置文件
- ibatis Cause: java.lang.ClassNotFoundException: Integer
- 浅析iPhone用户界面设计精粹,从细节成就卓越! (转载)
- SQL的存储语句
- python模块之uuid
- how to detect memory in memroy dump objects
- mysql 中 DATE_ADD(date,INTERVAL expr type)
- poj1251
- 在Qt Creator2.3中 qmake 建立 时设置自己的 环境变量!