python 的 ConfigParser 模块
来源:互联网 发布:spring源码 深度解析 编辑:程序博客网 时间:2024/05/21 06:49
configParser 模块用于操作特定格式的配置文件,该类配置文件可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
样例配置文件(/home/colben/git/python/conf/example_conf)
[book] title:ConfigParser模块教程 author:大头爸爸 email:366500050@qq.com time:2012-09-20 22:04:55 [size] size:1024 [other] blog:csdn.net
上面配置文件中用的是冒号,也可以用等号。
example.py文件
# -*- coding: utf-8 -*- import ConfigParser import string config=ConfigParser.ConfigParser() config.read(u'/home/colben/git/python/conf/example_conf') print string.upper(config.get("book","title")), print "by",config.get("book","author"), print "("+config.get("book","email")+")" print print config.get("size","size") print print config.sections() for section in config.sections(): print section for option in config.options(section): print " ",option,"=",config.get(section,option)
example.py文件执行结果
CONFIGPARSER模块教程 by 大头爸爸 (366500050@qq.com) 1024 ['book', 'size', 'other'] book title = ConfigParser模块教程 author = 大头爸爸 email = 366500050@qq.com time = 2012-09-20 22:04:55 size size = 1024 other blog = csdn.n
写入配置文件实例
import ConfigParser import sys config=ConfigParser.ConfigParser() config.add_section("book") config.set("book","title","这是标题") config.set("book","author","大头爸爸") config.add_section("size") config.set("size","size",1024) config.write(sys.stdout)
执行结果
[book] title = 这是标题 author = 大头爸爸 [size] size = 1024
ConfigParser方法
#1、创建ConfigParser实例 config=ConfigParser.ConfigParser() #2、返回配置文件中节序列 config.sections() #3、返回某个项目中的所有键的序列 config.options(section) #4、返回section节中,option的键值 config.get(section,option) #5、添加一个配置文件节点(str) config.add_section(str) #6、设置section节点中,键名为option的值(val) config.set(section,option,val) #7、读取配置文件 config.read(filename) #8、写入配置文件 config.write(obj_file)
综合实例
#coding=utf-8 import ConfigParser def writeConfig(filename): config = ConfigParser.ConfigParser() # set db section_name = 'db' config.add_section( section_name ) config.set( section_name, 'dbname', 'MySQL') config.set( section_name, 'host', '127.0.0.1') config.set( section_name, 'port', '80') config.set( section_name, 'password', '123456') config.set( section_name, 'databasename', 'test') # set app section_name = 'app' config.add_section( section_name ) config.set( section_name, 'loggerapp', '192.168.20.2') config.set( section_name, 'reportapp', '192.168.20.3') # write to file config.write( open(filename, 'a') ) def updateConfig(filename, section, **keyv): config = ConfigParser.ConfigParser() config.read(filename) print config.sections() for section in config.sections(): print "[",section,"]" items = config.items(section) for item in items: print "\t",item[0]," = ",item[1] print config.has_option("dbname", "MySQL") print config.set("db", "dbname", "11") print "..............." for key in keyv: print "\t",key," = ", keyv[key] config.write( open(filename, 'r+') ) if __name__ == '__main__': file_name = 'test.ini' writeConfig(file_name) updateConfig(file_name, 'app', reportapp = '192.168.100.100') print "end__"
0 0
- 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
- CocoaAsyncSocket学习
- Python 的 cx_Oracle 模块
- LEETCODE: Spiral Matrix II
- DWZ(JUI) 教程 左侧菜单初始化是关闭状态
- 第三届河南省程序设计大赛 - 部分题解
- python 的 ConfigParser 模块
- 树莓派变身路由器
- C语言文件IO操作
- ubuntu wubi setup
- 黑客培训系列流程
- LinearLayout显示分割线(Divider)
- 设置oracle归档模式时ORA-16033路径不一致
- python 的 logging 模块
- EntityFramework 延迟加载