【发布时间】:2014-03-30 20:31:03
【问题描述】:
我最近被介绍到库 configparser。我希望能够检查每个部分是否至少有一个布尔值设置为 1。例如:
[Horizontal_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 0
以上会导致错误。
[Vertical_Random_Readout_Size]
Small_Readout = 0
Medium_Readout = 0
Large_Readout = 1
以上会通过。以下是我想到的一些伪代码:
exit_test = False
for sections in config_file:
section_check = False
for name in parser.options(section):
if parser.getboolean(section, name):
section_check = True
if not section_check:
print "ERROR:Please specify a setting in {} section of the config file".format(section)
exit_test = True
if exit_test:
exit(1)
问题:
1) 如何执行第一个 for 循环并遍历配置文件的各个部分?
2) 这是一个好方法还是有更好的方法? (如果没有请回答第一个问题。)
【问题讨论】:
标签: python file configuration sections configparser