上例子:
假設配置文件如下,文件名爲test.cfg
#------------------------------------
#abcdefg
abc = 1
#-----------------------------------------
其中的#號後面的部分都是註釋
讀入該文件的python代碼爲:
from types import ModuleType
import re
cfg = []
fcfg = "D:/test.cfg"
content = {}
try:
execfile(fcfg, context)
except Exception,e:
......
for k,v in content:
if re.serch('^_',k) == None and not type(v) is ModuleType:
cfg[k] = v
這樣就完成解析
使用方法爲: cfg['abc']