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将内容写入配置文件。
3.基本例子
test.conf
[sec_a]
a_key1 = 20
a_key2 = 10
[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1
parse_test_conf.py
import ConfigParser
cf = ConfigParser.ConfigParser()
#read config
cf.read("test.conf")
# return all section
secs = cf.sections()
print 'sections:', secs
opts = cf.options("sec_a")
print 'options:', opts
kvs = cf.items("sec_a")
print 'sec_a:', kvs
#read by type
str_val = cf.get("sec_a", "a_key1")
int_val = cf.getint("sec_a", "a_key2")
print "value for sec_a's a_key1:", str_val
print "value for sec_a's a_key2:", int_val
#write config
#update value
cf.set("sec_b", "b_key3", "new-$r")
#set a new value
cf.set("sec_b", "b_newkey", "new-value")
#create a new section
cf.add_section('a_new_section')
cf.set('a_new_section', 'new_key', 'new_value')
#write back to configure file
cf.write(open("test.conf", "w"))
得到终端输出:
sections: ['sec_b', 'sec_a']
options: ['a_key1', 'a_key2']
sec_a: [('a_key1', "i'm value"), ('a_key2', '22')]
value for sec_a's a_key1: i'm value
value for sec_a's a_key2: 22
更新后的test.conf
[sec_b]
b_newkey = new-value
b_key4 = 127.0.0.1
b_key1 = 121
b_key2 = b_value2
b_key3 = new-$r
[sec_a]
a_key1 = i'm value
a_key2 = 22
[a_new_section]
new_key = new_value
4.Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
设定配置文件test2.conf
[portal]
url = http://%(host)s:%(port)s/Portal
host = localhost
port = 8080
使用RawConfigParser:
import ConfigParser
cf = ConfigParser.RawConfigParser()
print "use RawConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use RawConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出:
use RawConfigParser() read
http://%(host)s:%(port)s/Portal
use RawConfigParser() write
%(host)s:%(port)s
改用ConfigParser:
import ConfigParser
cf = ConfigParser.ConfigParser()
print "use ConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use ConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出:
use ConfigParser() read
http://localhost:8080/Portal
use ConfigParser() write
localhost:8080
改用SafeConfigParser:
import ConfigParser
cf = ConfigParser.SafeConfigParser()
print "use SafeConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use SateConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出(效果同ConfigParser):
use SafeConfigParser() read
http://localhost:8080/Portal
use SateConfigParser() write
localhost:8080
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- Python学习--01入门
Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- Python学习路径及练手项目合集
Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159
- python学习笔记-python程序运行
小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- [Python] 学习资料汇总
Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大且完善的通用型语言,已经有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用 ...
- Python学习之路【目录】
本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...
随机推荐
- 面向对象 "一"
1:面向对象不是所有情况都适用. 2面向对象编程 a:定义类 calss Foo: 注意顶一个类的时候首字母必须是大写 def (方法一)(self,bb) pass b:根据创建对象,创建和Foo实 ...
- 安全体系(二)——RSA算法详解
本文主要讲述RSA算法使用的基本数学知识.秘钥的计算过程以及加密和解密的过程. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(一)—— DES算法详解 1.概述 ...
- forEach、map、filter、some、every五个数组方法
forEach() 方法对数组的每一个元素执行一次提供的函数. map() 方法创建一个新数组,其结果是该数组都执行一次函数,原函数保持不变. filter() 方法使指定函数测试数组的每一个元素,并 ...
- 实战Tomcat配置SSL,使用openssl制作证书
制作证书以及Tomcat配置 搭建openssl环境,下载openssl并设置环境变量方便命令行的使用: 修改openssl配置文件,设置dir目录,如设置dir=e:/temp/openssl_ca ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- HTML超文本标记语言-基础标签整理
第一章 <META>标签: <meta http-equiv="Content-Type" Content="text/html;charset=gb2 ...
- 百度地图API-覆盖物
这两天一直在研究百度地图开放平台的各种好玩的东西,闲暇之余自己动手体验了一番,果然是妙趣横生,而且还可以自定义理想中的地图,不得不说,百度地图的开放平台为我们的应用提供了很多的便利,之前我们已经学习了 ...
- RxJava 笔记
前言 目前 RxJava 是安卓开发圈内最流行的库之一,本来准备研究研究,但看了扔物线写的<给 Android 开发者的 RxJava 详解>后,觉得生无可恋,哦不,是觉得已经写无可写. ...
- mac上的kindle打开mobi文件的方式
刚刚在macpro上下载了一个kindle文件,但是双击我某个目录下的.mobi文件,提示: “Kindle”打不开格式为“Kindle Document”的文件,可是明明手机和pad上是可以打开的. ...
- jquery实现名单滚动
转:http://www.qdfuns.com/notes/25341/917d9cb031f835a086dd445b77b6e04e.html 介绍:记录滚动特效.就是那一排文字不停地滚啊滚啊滚得 ...