解析配置檔案ConfigParser模組

2021-09-20 23:54:45 字數 1225 閱讀 4131

配置檔案內容:

[plain] view plain copy

[db]  

db_host=127.0.0.1  

db_port=3306  

db_user=root  

db_pass=password  

[concurrent]  

thread=10  

processor=20  

如果遵循以上格式,那麼就可以用python的configparser模組。

[python] view plain copy

#!/usr/bin/python  

# filename: parse_module.py  

import configparser  

filename = 'my.conf'  

parse = configparser.configparser()  

parse.read(filename)  

sections = parse.sections()  

for i in sections:  

options = parse.options(i)  

values = parse.items(i)  

print "section is %s " % i  

print "options are %s " % options  

print "values are %s " % values  

輸出:[plain] view plain copy

section is db   

options are ['db_host', 'db_port', 'db_user', 'db_pass']   

values are [('db_host', '127.0.0.1'), ('db_port', '3306'), ('db_user', 'root'), ('db_pass', 'password')]   

section is concurrent   

options are ['thread', 'processor']   

values are [('thread', '10'), ('processor', '20')]   

但是如果配置檔案中,parameter選項中含有,則無法用此模組進行解析。。因此當試用python作為開發語言時,配置檔案的格式可以選擇符合configparser模組的格式。。。

配置檔案解析函式

config.h this file is usred for parsing configure file.e mail yhniejun 163.com 2007.01.25 mr.nie the struct of config file.struct conf info typedef st...

tinyxml解析配置檔案

一 tinyxml類簡單介紹 tinyxml實現的時dom訪問模型,因此提供了一系列的類對應xml檔案中的各個節點。主要類間的關係如下圖所示 tixmlbase 其他類的基類,是個抽象類 tixmldocument 表示整個xml文件,不對應其中某個特定的節點。tixmlelement 表示元素節點...

DOM解析配置檔案

config.php header content type text html charset utf 8 doc new domdocument 1.0 utf 8 doc load config.xml 第一步獲得documentelement跟元素 config doc documentel...