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
转载至链接:https://my.oschina.net/zhiyonghe/blog/1860383