比较没啥卵用的configparser模块讲解

缺点:固定的形式,不常见,适用于python

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变量的解析。

[portal]
url = http://%(host)s:%(port)s/Portal
host = localhost
port = 8080

设定配置文件test2.conf

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")

使用RawConfigParser:

use RawConfigParser() read
http://%(host)s:%(port)s/Portal
use RawConfigParser() write
%(host)s:%(port)s

得到终端输出:

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")

改用ConfigParser:

use ConfigParser() read
http://localhost:8080/Portal
use ConfigParser() write
localhost:8080

得到终端输出:

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")

改用SafeConfigParser:

得到终端输出(效果同ConfigParser):

use SafeConfigParser() read
http://localhost:8080/Portal
use SateConfigParser() write
localhost:8080
时间: 2024-09-06 23:06:13

比较没啥卵用的configparser模块讲解的相关文章

Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识

今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过运算得到一串hash如果把hash算法比喻一座工厂 那传给hash算法的内容就是原材料,生产的hash值就是生产出的产品 为何用hash算法: hash值产品有三大特性: 1.只要传入的内容一样,得到的hash值必然是一样的 2.只要我们使用的hash算法固定,无论传入的内容有多大得到的hash值得

25.Python序列化模块,hashlib模块, configparser模块,logging模块,异常处理

一.序列化模块 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给?现在我们能想到的方法就是存在文件里,然后另一个python程序再从文件里读出来.但是我们都知道,对于文件来说是没有字典这个概念的,所以我们只能将数据转换成字典放到文件中.你一定会问,将字典转换成一个字符串很简单,就是str(dic)就可以办到了,为什么我们还要学习序列化模块呢?没错序列化的过程就是从dic 变成str(di

python ConfigParser模块 配置文件解析

ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic

用ConfigParser模块读写配置文件——Python

对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑的问题. 2)用配置文件,通过在程序中读配置文件获取配置,用户改变配置后重新写入配置文件,即使程序崩溃或者关闭,配置依然能够保存下来. 3)用数据库来存储配置变量,也能长久保存,不过读写数据库也是重量级操作,不太方便. 最近在写一个基于wxPython的GUI程序,需要用到配置文件,本来打算用xml

Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块

一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.copyfileobj(open("old_test.txt","r"),open("new_test.txt","w")) 输出结果 2.shutil.copyfile(src,dst) 复制文件内容到另外一个文件,不需要打开文件,

python学习-shutil,configparser模块

shutil模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. shutil.copyfileobj(fsrc, fdst[, length]) shutil.copyfileobj(fsrc, fdst[, length]):复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数length指定缓冲区的大小,负数表示一次性读入.默认会把数据切分成小块拷贝,以免占用太多内存.注意:拷

Python中ConfigParser模块应用

Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser,ConfigParser和SafeConfigParser.其中RawCnfigParser是最基础的INI文件读取类,ConfigParser.SafeConfigParser支持对%(value)s变量的解析. 下面看看怎样通过ConfigParser类来解析一个ini文件. 配置文件settings.cfg [DEFAULT] myk

day6 ConfigParser模块 yaml模块

    yaml模块: python可以处理yaml文件,yaml文件安装的方法为:$ pip3 install pyyaml    configparser模块,用来处理文件的模块,可以实现文件的增删改查 configparser用于处理特定格式的文件,其本质上是利用open来操作文件 下面来看看configarser模块的功能: [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx

python 之ConfigParser模块学习

1.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类型 1