python configParser模块详解(转载)
来源:互联网 发布:女生风油精滴下体 知乎 编辑:程序博客网 时间:2024/06/05 05:25
本文转载自http://blog.csdn.net/zm2714/article/details/8002125
configParser 模块用于操作配置文件
注:Parser汉译为“解析”之意。
配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
为了更好的理解本文,我们先了解一下配置文件的组成及命名:配置文件(INI文件)由节(section)、键、值组成。
样例配置文件example.ini
- [book]
- title:ConfigParser模块教程
- time:2012-09-20 22:04:55
- [size]
- size:1024
- [other]
- blog:csdn.net
[book]title:ConfigParser模块教程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’d:/百度网盘/android/Python/python_example/sample.ini’)
- print string.upper(config.get(“book”,“title”)),
- print “by”,config.get(“book”,“author”),
- print “(“+config.get(“book”,“email”)+“)”
- print config.get(“size”,“size”)
- print config.sections()
- for section in config.sections():
- print section
- for option in config.options(section):
- print “ ”,option,“=”,config.get(section,option)
# -*- coding: utf-8 -*-import ConfigParserimport stringconfig=ConfigParser.ConfigParser()config.read(u'd:/百度网盘/android/Python/python_example/sample.ini')print string.upper(config.get("book","title")),print "by",config.get("book","author"),print "("+config.get("book","email")+")"printprint config.get("size","size")printprint config.sections()for section in config.sections(): print section for option in config.options(section): print " ",option,"=",config.get(section,option)
example.py执行结果
- C:\Documents and Settings\Administrator>tmp.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.net
C:\Documents and Settings\Administrator>tmp.pyCONFIGPARSER模块教程 by 大头爸爸 (366500050@qq.com)1024['book', 'size', 'other']book title = ConfigParser模块教程 author = 大头爸爸 email = 366500050@qq.com time = 2012-09-20 22:04:55size size = 1024other blog = csdn.net
写配置文件实例
- 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)
import ConfigParserimport sysconfig=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
[book]title = 这是标题author = 大头爸爸[size]size = 1024
ConfigParser方法
- 1、config=ConfigParser.ConfigParser()
- 创建ConfigParser实例
- 2、config.sections()
- 返回配置文件中节序列
- 3、config.options(section)
- 返回某个项目中的所有键的序列
- 4、config.get(section,option)
- 返回section节中,option的键值
- 5、config.add_section(str)
- 添加一个配置文件节点(str)
- 6、config.set(section,option,val)
- 设置section节点中,键名为option的值(val)
- 7、config.read(filename)
- 读取配置文件
- 8、config.write(obj_file)
- 写入配置文件
1、config=ConfigParser.ConfigParser()创建ConfigParser实例2、config.sections()返回配置文件中节序列3、config.options(section)返回某个项目中的所有键的序列4、config.get(section,option)返回section节中,option的键值5、config.add_section(str)添加一个配置文件节点(str)6、config.set(section,option,val)设置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__”
#coding=utf-8import ConfigParserdef 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 模块学习
- RecyclerView的极尽升华:BaseRecyclerViewFragment
- IE与非IE浏览器调用PC摄像头拍摄并且上传
- Windows驱动开发之入门篇(一)
- mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题
- 百度地图api 常用demo
- python configParser模块详解(转载)
- Java线程面试题 Top 50 (转载)
- 窗体程序计算一元二次方程
- 主线程中的Looper.loop()一直无限循环为什么不会造成ANR?
- 使用百度地图JavaScript API构建离线地图应用(完整教程)
- Excel 统计符合条件不重复的个数
- Xenial中安装sougou拼音遇到不兼容问题
- 百度地图调用及电子围栏设置
- 一图看懂运维工程师必备技能~