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. docker rmi 详解

    docker rmi 用于删除指定的镜像,常见用法如下: [root@localhost ~]$ docker rmi centos # 根据 REPOSITORY 来删除镜像 [root@local ...

  2. Android使用百度定位API时获取的地址信息为null

    option.setAddrType("all"); //加上这个配置后才可以取到详细地址信息

  3. 《C++ Primer Plus》14.4 类模板 学习笔记

    14.4.1 定义类模板下面以第10章的Stack类为基础来建立模板.原来的类声明如下:typedef unsigned long Item; class Stack{private:    enum ...

  4. 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...

  5. c++11——type_traits 类型萃取

    一. c++ traits traits是c++模板编程中使用的一种技术,主要功能:     把功能相同而参数不同的函数抽象出来,通过traits将不同的参数的相同属性提取出来,在函数中利用这些用tr ...

  6. Android PackageManager:queryIntentActivities

    Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.setPackage(getPackageName()); intent.ad ...

  7. SRC常见WEB漏洞系列之HTTP-HOST头攻击

    一.背景: web程序需要知道网站的域名比较麻烦,需要使用HTTP的 host头字段: <?php _SERVER["HTTP_HOST"] ?> @app.route ...

  8. jquery背景backgroundPosition插件

    在jquery官网里找到(http://plugins.jquery.com/kbw.backgroundpos/) 语法: obj.animate({'background-position': ' ...

  9. MYSQL创建数据表!

    几个常见的建表原则: a,表都加前缀b,所有的字段选择最小的数据类型,如id可以使用mediumint比INT节省25%的空间c,尽量所有的字段都设置为NOT NULL的,这样能让速度更快d,为合适的 ...

  10. Android官方架构组件介绍之ViewModel

    ViewModel 像Activity,Fragment这类应用组件都有自己的生命周期并且是被Android的Framework所管理的.Framework可能会根据用户的一些操作和设备的状态对Act ...