ConfigParser模块使用
来源:互联网 发布:电脑数据恢复专家 编辑:程序博客网 时间:2024/06/07 06:41
ConfigParser模块,这个模块是操作配置文件的,具有读取,添加,更改等功能。
>>> import ConfigParser>> config = ConfigParser.ConfigParser()>>> config.read('application.cfg')#直接读取配置文件内容['application.cfg']>>> config.sections()#得到所有的section,并以列表的形式返回['appserver', 'modules_installed']>>> config.options('appserver')#得到该section的所有option['server_port', 'server_ip']>>> config.items('appserver')#得到该section的所有键值对[('server_port', '8100'), ('server_ip', '0.0.0.0')]>>> config.get('appserver','server_port',)#得到section中的option的健直'8100'>>> config.getint('appserver','server_port',)#对section中的option进行设置,需要调用write将内容写入配置文件。2006#还有>>> config.getfloat()>>> config.getboolean()>>> config.add_section('NEW')#添加一个新的section>>> config.remove_section('NEW') #移出sectionTrue>>> config.set('appserver','server_port','2006')#更改option内容>>> config.set('NEW','liuqing','int')#也可用以新添加option>>> config.remove_option('NEW','liuqing')#移除section中的option项True>>> config.has_section('NEW')#判断section是否存在True>>> config.has_option('NEW','liuqing')#判断section中的option是否存在 False>>> config.write(open("application.cfg", "w"))#把这些改变写回,不然文件不变
其中sections就是中括号[]中的内容,options指sections区域中的健。
配置文件样式如下:
*application.cfg*[appserver]server_port = 8100server_ip = 0.0.0.0[modules_installed]base = {'name': u'', 'url_prefix': '/base'}haproxy = {'name': u'', 'url_prefix': '/haproxy'}monitor = {'name': u'', 'url_prefix': '/monitor'}
借鉴于
http://www.pythontab.com/html/2014/pythonhexinbiancheng_1120/919.html
python指导手册中介绍
https://docs.python.org/2/library/configparser.html
0 0
- ConfigParser模块使用
- 使用ConfigParser模块解析配置文件
- python中ConfigParser模块使用
- ConfigParser模块
- configparser模块
- configparser模块
- configparser模块
- Python中ConfigParser模块的使用
- 【Python】配置文件解析模块ConfigParser使用实例
- 使用ConfigParser模块解析普通文件
- python3配置文件模块configparser.ConfigParser
- ConfigParser 使用
- 使用python的ConfigParser模块方便的读取配置文件
- (转)Python中的ConfigParser模块使用详解
- PYTHON 配置文件模块 ConfigParser
- Python模块之: ConfigParser
- python 之ConfigParser模块
- Python ConfigParser模块
- 这段时间一直在做iconfont.cn这个平台。积累了些东西,记录下。 1.什么是iconfont? iconfont不是什么新技术了。 我们知道web网页能使用的字体一直很少,很多时候设计师用ps
- fast_rcnn
- Java正则表达式
- [Error] 'atoi' was not declared in this scope
- LeetCode 134. Gas Station
- ConfigParser模块使用
- 动态规划方法解决最大连续子序列和
- 剑指offer之面试题9-3:变态跳台阶
- 宾克斯的美酒(歌词和发音)---海贼王-布鲁克和拉布的回忆篇的音乐。
- IOS汉字与UTF8码的互换
- 剑指offer 二叉树的镜像
- Android的视频通话的Java代码 anychat
- 系统声音服务的使用(播放声音、震动)
- 用CADisplayLink做雪花飘飞的效果