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 ConfigParser的使用的更多相关文章

  1. python configparser模块

    来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...

  2. python ConfigParser配置读写

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...

  3. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  4. [Python]ConfigParser解析配置文件

    近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...

  5. Python configparser 读取指定节点内容失败

    # !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...

  6. python configparser使用

    .ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成. 以 example.ini为例: [DEFAULT] ServerAliveInterval = 45 Co ...

  7. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  8. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  9. 【转载】Python ConfigParser的使用

    1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...

随机推荐

  1. iOS - UIScrollView xib添加约束的坑

    一.Storyboard中的UIScrollView使用自动布局 在使用storyboard和xib时,我们经常要用到ScrollView,还有自动布局AutoLayout,但是ScrollView和 ...

  2. Swift-属性、方法、下标

    存储属性和计算属性 类.结构和枚举都能够定义存储属性和计算属性.其中存储属性就是常见的形式,又分为变量属性和常量属性,如: struct Point { var x = 0.0, y = 0.0 } ...

  3. Computer Science Theory for the Information Age-5: 学习理论——VC维的定义以及一些例子

    学习理论——VC维的定义以及一些例子 本文主要介绍一些学习理论上的东西.首先,我们得明确,从训练集上学习出来的分类器的最终目标是用于预测未知的样本,那么我们在训练的时候该用多少的样本才能使产生的分类器 ...

  4. 邮件欺诈与SPF防御

    一.邮件欺诈: 众所周知,现在邮件的发件人是自己生成的,其实发件域名也是可以自己生成的.例如,A得知B组织的邮箱域(前提是B组织邮箱域没有配置SPF),那么A可以自己起一个邮箱服务器,配置相同的域名. ...

  5. video事件

    /** video播放器*/ * @ src: 指定所要嵌入视频.文档的URL. * @ poster: 视频预览图像 * @ autoplay: 视频自动播放 * @ loop: 循环播放 * @ ...

  6. strace命令的使用

    author: headsen  chen date: 2018-08-28   21:25:48 跟踪一个命令的过程: [root@zabbix-test ~]# yum -y install st ...

  7. Listview Section 多个标题以及内容

    其中日期标题部分视图布局: 带图片的条目布局部分: 问题在于,如何在ListView中既有标题条目又有内容条目. 这里用到了设计模式中的Iterator模式.在java代码中示例有Iterator,可 ...

  8. WCF学习 (三)深入认识WCF契约

    什么是契约? 从SOA概念上讲,契约属于服务公开接口的一部分.一个服务契约,定义了服务端公开的服务方法,使用传输协议,可访问地址,传输的消息格式等内容.换句话说:契约描述了该服务的功能和作用,它告诉S ...

  9. BeginnerAdmin后台框架的使用!

    基于layui的后台框架,我比较喜欢使用BeginnerAdmin模板.但是在构建的时候可能会遇到一些问题. 问题一:侧栏出不来: 报错是这样的: 这个错误:说明的是 navbar.js没有引进来. ...

  10. 微信小程序 --- 设置页面的标题

    第一种方式:修改 page.json文件 { "navigationBarTitleText": "豆瓣 - 电影" } 第二种方式:使用 JS 修改: wx. ...