python -ConfigParser模块讲解
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
上面配置文件中用的是冒号,也可以用等号。
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)
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
写配置文件实例
- 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、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__"
python -ConfigParser模块讲解的更多相关文章
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- PyYAML和configparser模块讲解
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...
- python ConfigParser模块 配置文件解析
ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...
- 【python】python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Python - configParser模块学习
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- python ConfigParser 模块
ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...
- python configparser模块
来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...
- python itertools 模块讲解
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...
随机推荐
- Android滑动冲突解决方法
叙述 滑动冲突可以说是日常开发中比较常见的一类问题,也是比较让人头疼的一类问题,尤其是在使用第三方框架的时候,两个原本完美的控件,组合在一起之后,忽然发现整个世界都不好了. 关于滑动冲突 滑动冲突分类 ...
- 设计模式4---原型模式(Prototype Pattern)
原型模式(Prototype):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式结构图 通俗来说:原型模式就是深拷贝和浅拷贝的实现. 浅拷贝 只实现了值拷贝,对于引用对象还是 ...
- ES 遇到的一个坑too_many_clauses: maxClauseCount
异常: Caused by: org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: too_many_clauses: ...
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- 安装pygame for Python3.5
首先进入pygame官网下载页面:http://www.pygame.org/download.shtml 页面上提供的有py2.4-py2.7,py3.1-py3.2的版本(py2.4就是对应py ...
- DLL用def定义文件来导出重载函数(转)
动态链接库DLL_Sample.dll DLL_Sample.h:#ifdef TEST_API# define TEST_API _declspec(dllexport)#else# define ...
- Web开发入门经典:使用PHP6、Apache和MySQL 中文pdf扫描版
通过学习本书,读者很快就能明白为什么PHP.Apache和MySQL会迅速成为开发动态网站最流行的方式,本书将为读者理解这3个核心组件如何独立工作和协同工作奠定良好的基础,引导读者充分利用它们提供的各 ...
- [LeetCode 题解]: Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- vmware获取主机、数据中心等对象ManagedObjectReference
在vmware的api中提供以下列表中的对象,称作ManagedObjectReference,包括虚拟机信息.主机.数据中心等等一些信息,我们可以通过vcenter的web api得到. 下面我们来 ...
- SQLServer客户端连接工具(支持2000,20005,2008)
绿色版本, 体积小(不到2M), 支持数据库版本2000 2005 2008 界面仿最经典的SQLServer2000: 下载地址:http://download.csdn.net/detail/gg ...