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学习_ConfigParse (转)的更多相关文章

  1. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  2. Python学习--01入门

    Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...

  3. Python 学习小结

    python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...

  4. Python学习路径及练手项目合集

    Python学习路径及练手项目合集 https://zhuanlan.zhihu.com/p/23561159

  5. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  6. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  7. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  8. [Python] 学习资料汇总

    Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大且完善的通用型语言,已经有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用 ...

  9. Python学习之路【目录】

    本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...

随机推荐

  1. 转:frame和iframe的区别

    1.frame不能脱离frameSet单独使用,iframe可以: 2.frame不能放在body中:如下可以正常显示: <!--<body>--> <frameset ...

  2. CSS常用字体Unicode 编码

    在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误. 为此,在 CSS 直接使用 Unicode 编码来写字体名称可以避免这些错误. ...

  3. 关于 myBatis 中的 jdbcType的细节问题

    前几天上线了一个 版本,第二天到公司的时候,在cat上发现了一个长sql查询非常耗时,几乎把线上的项目搞崩溃了.我马上开始排查问题.最终发现 球队的ID传过来的时候是String 类型的,但是在执行s ...

  4. HA高可用集群

     准备2台机器:主:master:192.168.254.140从:slave:192.168.254.141 1.主上安装:wget www.lishiming.net/data/attachmen ...

  5. QMediaPlayer占用CPU过高问题

    根据搜索引擎的结果,要想实现QT下播放多媒体,一般有两种方案:一种是使用第三方插件,好像叫Phonon:一种是使用QT自带的QMediaplayer. 两种方法各有利弊.使用第三方插件,则方便易用,封 ...

  6. 在.NET项目中使用PostSharp,实现AOP面向切面编程处理

    PostSharp是一种Aspect Oriented Programming 面向切面(或面向方面)的组件框架,适用在.NET开发中,本篇主要介绍Postsharp在.NET开发中的相关知识,以及一 ...

  7. 1131: [POI2008]Sta

    1131: [POI2008]Sta Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 783  Solved: 235[Submit][Status] ...

  8. #MainTest

    '界面设计' --- <TextView android:layout_width="wrap_content" android:layout_height="wr ...

  9. android开发之多线程实现方法概述

    一.单线程模型 当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件, ...

  10. 读书笔记 effective c++ Item 34 区分接口继承和实现继承

    看上去最为简单的(public)继承的概念由两个单独部分组成:函数接口的继承和函数模板继承.这两种继承之间的区别同本书介绍部分讨论的函数声明和函数定义之间的区别完全对应. 1. 类函数的三种实现 作为 ...