python 之ConfigParser
- ConfigParser 简介
ConfigParser是用来操作配置文件的模块。
说明:[**]为配置文件的section,基本格式为
[section]
key = value
eg:
[db]
db_host = 127.0.0.1
db_port = 22
db_user = root
db_pass = rootroot
[concurrent]
thread = 10
processor = 20 - eg
#-*- encoding:UTF-8 -*-
import ConfigParser def main():
#初始化
cf = ConfigParser.ConfigParser()
cf.read("test.conf") # 获得所有的section
section = cf.sections()
print 'section:', section #获取指定section下所有的option(key)
keys = cf.options("db")
print 'options:', keys #获取指定section的option的值
value = cf.items("db")
print 'db:', value #可以按照类型读取出来,同样的还有getfloat、getboolean
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 #修改一个option(key)的值,(记得写回去)
cf.set("db", "db_pass", "hanhong")
cf.write(open("test.conf", "w")) #添加一个section。(写回去)
cf.add_section('liuqing')
cf.set('liuqing', 'int', '')
cf.set('liuqing', 'bool', 'true')
cf.set('liuqing', 'float', '3.1415')
cf.set('liuqing', 'baz', 'fun')
cf.set('liuqing', 'bar', 'Python')
cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
cf.write(open("test.conf", "w")) #移除section 或者option 。(只要进行了修改就要写回)
cf.remove_option('liuqing','int')
cf.remove_section('liuqing')
cf.write(open("test.conf", "w")) if __name__ == "__main__":
main()输出:
section: ['db', 'concurrent']
options: ['db_host', 'db_port', 'db_user', 'db_pass']
db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]
db_host: 127.0.0.1
db_port: 22
db_user: root
db_pass: rootroot
thread: 10
processor: 20
python 之ConfigParser的更多相关文章
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python之ConfigParser的使用。
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
随机推荐
- [Tools] 远程登录surface字体过大解决方法
[背景] 这两天一直远程登录surface,使用surface干活,每次分辨率超大,看着就感觉像显示驱动没装好似的,必须得找个办法来设置 [开工] 在surface上设置低一点的分辨率,成功,但是远程 ...
- 信号量互斥,王明学learn
信号量互斥 信号量(又名:信号灯)与其他进程间通信方式不大相同,主要用途是保护临界资源(进程互斥).进程可以根据它判定是否能够访问某些共享资源.除了用于访问控制外,还可用于进程同步. 一.信号量分类 ...
- 在Salesforce中实现对Object的增删改查操作
1): Insert Statement http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=ap ...
- Socket编程基础知识
端口号常识: 端口号被从1 开始分配. 通常端口号超出255 的部分被本地主机保留为私有用途. 1到255 之间的号码被用于远程应用程序所请求的进程和网络服务. 每个网络通信循环地 ...
- Mock方法介绍
1 现有的单元测试框架单元测试是保证程序正确性的一种有效的测试手段,对于不同的开发语言,通常都能找到相应的单元框架. 借助于这些单测框架的帮助,能够使得我们编写单元测试用例的过程变得便捷而优雅.框架帮 ...
- js-Ajax与Comet
Ajax与Comet: 1.Ajax技术的核心是XHR(XMLHTTPRequest对象) 创建xhr对象: function createXHR(){ if(typeof XMLHttpReques ...
- 转:ExpressBars中的停靠控件使用
http://www.cnblogs.com/jxsoft/archive/2011/08/25/2152872.html 1 新手上路 1.1 控件简介 Dock pan ...
- How to retrieve instance parameters from an uninstantiated (uninserted) family
The trick to be able to read the default values for instance parameters is to get to the FamilyManag ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- BZOJ1894 : Srm444 avoidfour
首先只有质数个$4$且个数不超过$10$的限制条件才有用, 也就是长度不能为$44,444,44444,4444444$的倍数. 考虑容斥,计算长度必须是它们$lcm$的倍数,且没有连续$4$个$4$ ...