python ConfigParser
来源:互联网 发布:梦想海贼王超进化数据 编辑:程序博客网 时间:2024/05/16 11:17
http://www.codesky.net/article/201003/122500.html
http://www.linuxso.com/linuxbiancheng/8987.html
以下的文章就是对Python 读写配置文件的具体方案的介绍
1,函数介绍
1.1.读取配置文件
-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型
1.2.写入配置文件
-add_section(section) 添加一个新的section-set( section, option, value) 对section中的option进行设置
需要调用write将内容写入配置文件。
2,测试实例
2.1,测试1
配置文件test.cfg
测试文件test.py2.2,测试2
测试文件test.py
分析
其中[ ] 中的info是这段配置的名字。
其中age,name都是属性。
首先,config=ConfigParser.ConfigParser() 得到一个配置config对象.下面打开一个配置文件 cfgfile. 用readfp()读取这个文件.这样配置的内容就读到config对象里面了。
接下来一个问题是如何读取值.常用的方法是get() 和getint() . get()返回文本. getint()返回整数。
其次,name=config.get(''info'',''name'') 意思就是.读取config中info段中的name变量值。
最后讲讲如何设置值.使用set(段名,变量名,值) 来设置变量.config.set(''info'',''age'',''21'') 表示把info段中age变量设置为21。
2.3,测试3
Python的ConfigParser Module中定义了3个类对INI文件进行操作。
分别是RawConfigParser、ConfigParser、SafeConfigParser。
RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
配置文件test.cfg使用RawConfigParser:得到输出
改用ConfigParser
得到输出
改用SafeConfigParser,效果与ConfigParser相同
结论:
还是用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模块
- Python ConfigParser的使用
- 群论初步
- IOS-瀑布流(核心算法以及实践)
- java 监控mysql执行sql
- create a 3D map
- shell脚本练习(三)
- python ConfigParser
- Caffe学习系列:用训练好的caffemodel来进行分类
- Android官方开发文档Training系列课程中文版:管理系统UI之变暗系统条
- 多校&&HDU.5733 tetrahedron
- 简单文件搜索器的实现
- 去掉UICollectionView 在 reloadItems时默认附加的隐式fade动画。
- AngularJS学习笔记一
- POJ 1321 棋盘问题
- jQuery下拉框使用(一)