【conf】配置文件入门 ConfigParser for Py2.7.6
来源:互联网 发布:mac book多少钱 编辑:程序博客网 时间:2024/05/22 02:27
声明:本文转载自windone0109@csdn,原出处不可考,故仅做出我看到的这则转载的传送门:【这里】
一、ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:
[db]db_host = 127.0.0.1db_port = 22db_user = rootdb_pass = rootroot[concurrent]thread = 10processor = 20
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
cf = ConfigParser.ConfigParser()cf.read("配置文件名")
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
s = cf.sections()print 'section:', s将输出(以下将均以简介中配置文件为例):
section: ['db', 'concurrent']
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
o = cf.options("db")print 'options:', o将输出:
options: ['db_host', 'db_port', 'db_user', 'db_pass']
3. 获取指定section 的配置信息。
v = cf.items("db")print 'db:', v将输出:
db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]
4. 按照类型读取指定section 的option 信息。
同样的还有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_hostprint "db_port:", db_portprint "db_user:", db_userprint "db_pass:", db_passprint "thread:", threadsprint "processor:", processors
将输出:
db_host: 127.0.0.1
db_port: 22
db_user: root
db_pass: rootroot
thread: 10
processor: 20
5. 设置某个option 的值。(记得最后要写回)
cf.set("db", "db_pass", "zhaowei")cf.write(open("test.conf", "w"))
6.添加一个section。(同样要写回)
cf.add_section('liuqing')cf.set('liuqing', 'int', '15')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"))
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
cf.remove_option('liuqing','int')cf.remove_section('liuqing')cf.write(open("test.conf", "w"))
#!/usr/bin/env pythonfrom ConfigParser import ConfigParserCONFIGFILE="f.txt"config=ConfigParser()config.read(CONFIGFILE)print config.get('messages','greeting')radius=input(config.get('messages','questions')+' ')print config.get('messages','result')print config.getfloat('numbers','pi')*radius**2s=config.sections()print'section: ',so=config.options('messages')print'messages option: ',ov=config.items("messages")print'message de xinxi: ',vconfig.add_section('liuyang1')config.set('liuyang1','int','15')config.set('liuyang'1,'hhhh','hello world')config.write(open("f.txt","w"))print config.get('liuyang1','int')print config.get('liuyang1','hhhh')
#!/usr/bin/env pythonimport ConfigParserimport sysconfig=ConfigParser.ConfigParser()config.add_section("book1")config.set("book1","title","hello world")config.set("book1","aut","log")config.write(open("f.txt","w"))
0 0
- 【conf】配置文件入门 ConfigParser for Py2.7.6
- python读conf配置文件--ConfigParser
- python读conf配置文件--ConfigParser
- python读conf配置文件--ConfigParser
- python读conf配置文件--ConfigParser
- python读conf配置文件--ConfigParser
- python3配置文件模块configparser.ConfigParser
- wxpython for py2.7
- PYTHON 配置文件模块 ConfigParser
- [Python]ConfigParser解析配置文件
- 解析配置文件ConfigParser模块
- python ConfigParser操作配置文件
- 【python】ConfigParser---读写配置文件
- 读写配置文件ConfigParser
- ConfigParser 读取 config 配置文件
- Python3 配置文件(configparser)
- Python configparser修改配置文件
- python读取配置文件configparser
- with root cause java.lang.NoClassDefFoundError:Could not initialize class com.AlipaySubmit
- 使用Framework里的类但不打包到apk中
- Android:alpha换算表
- twisted+sftp
- 解决打开IE无反应的问题
- 【conf】配置文件入门 ConfigParser for Py2.7.6
- Android LayoutInflater详解
- 没有伞的孩子必须努力奔跑!
- BLOCK
- layout_marginBottom 无效,解决办法
- OpenCV系列之Canny边缘算子
- 一次date_add引发的命案
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- 引用初始化(临时对象)