Python 迭代配置文件中的部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22068050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:10:07  来源:igfitidea点击:

Iterate over sections in a config file

pythonfileconfigurationsectionsconfigparser

提问by Marmstrong

I recently got introduced to the library configparser. I would like to be able to check if each section has at least one Boolean value set to 1. For example:

我最近被介绍到库 configparser。我希望能够检查每个部分是否至少有一个布尔值设置为 1。例如:

[Horizontal_Random_Readout_Size]
Small_Readout  = 0
Medium_Readout = 0
Large_Readout  = 0

The above would cause an error.

以上会导致错误。

[Vertical_Random_Readout_Size]
Small_Readout  = 0
Medium_Readout = 0
Large_Readout  = 1

The above would pass. Below is some pseudo code of what I had in mind:

以上都会通过。下面是我想到的一些伪代码:

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)

Questions:

问题:

1) How do I perform the first for loop and iterate over the sections of the config file?

1) 如何执行第一个 for 循环并遍历配置文件的各个部分?

2) Is this a good way of doing this or is there a better way? (If there isn't please answer question one.)

2)这是这样做的好方法还是有更好的方法?(如果没有请回答问题一。)

采纳答案by Nilesh

Using ConfigParseryou have to parse your config.

使用ConfigParser你必须解析你的配置。

After parsing you will get all sections using .sections()method.

解析后,您将使用.sections()方法获得所有部分。

You can iterate over each section and use .items()to get all key/value pairs of each section.

您可以遍历每个部分并用于.items()获取每个部分的所有键/值对。

for each_section in conf.sections():
    for (each_key, each_val) in conf.items(each_section):
        print each_key
        print each_val

回答by user3360243

Best bet is to load ALL the lines in the file into some kind of array (I'm going to ignore the issue of how much memory that might use and whether to page through it instead).

最好的办法是将文件中的所有行加载到某种数组中(我将忽略可能使用多少内存以及是否要翻页的问题)。

Then from there you know that lines denoting headings follow a certain format, so you can iterate over your array to create an array of objects containing the heading name; the line index (zero based reference to master array) and whether that heading has a value set.

然后从那里您知道表示标题的行遵循某种格式,因此您可以遍历数组以创建包含标题名称的对象数组;行索引(对主数组的从零开始的引用)以及该标题是否设置了值。

From there you can iterate over these objects in cross-reference to the master array, and for each heading check the next "n" lines (in the master array) between the current heading and the next.

从那里,您可以在与主数组的交叉引用中迭代这些对象,并为每个标题检查当前标题和下一个标题之间的下一个“n”行(在主数组中)。

At this point you're down to the individual config values for that heading so you should easily be able to parse the line and detect a value, whereupon you can break from the loop if true, or for more robustness issue an exclusivity check on those heading's values in order to ensure ONLY one value is set.

在这一点上,您可以查看该标题的各个配置值,因此您应该能够轻松解析该行并检测到一个值,如果为真,您可以中断循环,或者为了更稳健,对这些值进行排他性检查标题的值,以确保只设置一个值。

Using this approach you have access to all the lines, with one object per heading, so your code remains flexible and functional. Optimise afterwards.

使用这种方法,您可以访问所有行,每个标题一个对象,因此您的代码保持灵活和功能。后期优化。

Hope that makes sense and is helpful.

希望这是有道理的,是有帮助的。