最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

Python解析配置模块之ConfigParser详解

来源:懂视网 责编:小采 时间:2020-11-27 14:28:34
文档

Python解析配置模块之ConfigParser详解

Python解析配置模块之ConfigParser详解:1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section的所有option-items(section) 得到该section的所有键值对-get(section,option)
推荐度:
导读Python解析配置模块之ConfigParser详解:1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section的所有option-items(section) 得到该section的所有键值对-get(section,option)

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

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

Python解析配置模块之ConfigParser详解

Python解析配置模块之ConfigParser详解:1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section的所有option-items(section) 得到该section的所有键值对-get(section,option)
推荐度:
标签: 模块 详解 解析
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top