您的位置:首页 > 编程语言 > Python开发

Python解析配置文件模块:ConfigPhaser

2018-07-20 22:22 399 查看
import configparser as  pa
# [SectionA]
# a = aa
# b = bb
# c = cc

# [SectionB]
# optionint = 1
# optionfloat = 1.1
# optionstring = string
#https://www.cnblogs.com/anpengapple/p/5095133.html

with open(r"E://test.ini", 'r') as fr:
cfg=pa.ConfigParser()
cfg.read_file(fr)
secs = cfg.sections()
print(secs)  #['SectionA', 'SectionB']
ops0 = cfg.options(secs[0]) #['a', 'b', 'c']

ops1 = cfg.items(secs[1])
print (ops0) #['a', 'b', 'c']
print (ops1)  #[('optionint', '1'), ('optionfloat', '1.1'), ('optionstring', 'string')]

print( cfg.getint(secs[1], 'optionint') )
print(cfg.get(secs[1],'optionstring'))

输出结果:

['SectionA', 'SectionB']
['a', 'b', 'c']
[('optionint', '1'), ('optionfloat', '1.1'), ('optionstring', 'string')]
1
string


方法解释说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python