Python 标准模块 ConfigParser(转)
来源:互联网 发布:人工智能包括哪些方面 编辑:程序博客网 时间:2024/06/05 06:56
20160520update: 最省事的还是读JSON
#=====================================
发现python读写配置文件比较方便。
import ConfigParser
接口:ConfigParser.ConfigParser()
读写文件:read write
结构: config file =>sections=>options=>items
读取某个option的值(str): get(section,option)
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类型,还有相应的getboolean()和getfloat() 函数。
2.基本的写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
程序如下:
#/usr/bin/python
import ConfigParser
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read("test.conf")
#return all section
secs = cf.sections()
print 'sections:', secs
opts = cf.options("db")
print 'options:', opts
kvs = cf.items("db")
print 'db:', kvs
#read by type
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")
#read int
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
#modify one value and write to file
cf.set("db", "db_pass", "xgmtest")
cf.write(open("test.conf", "w"))
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模块详解
- 杂题(好题)
- 迁移表空间到指定路径
- Android如何自动校验版本号,渠道号,签名和混淆
- 3D数学 数学通用函数库源代码(附中文注释)
- 正则表达式入门
- Python 标准模块 ConfigParser(转)
- LeetCode Bulls and Cows 数组
- Android,App 常用图标尺寸规范
- UnKnownHostException
- STD 1009 Elevator
- string 转stream和stream转string
- Java源代码各个代码块的加载顺序
- nutch 1.6 安装与配置
- Android线程封装基类Thread