简体   繁体   English

configObj部分上的选择性迭代

[英]Selective iteration over configObj sections

I'm having some issues iterating over sections in a configObj object. 我在遍历configObj对象中的部分时遇到一些问题。 What I'm trying to do is iterate over a subset of the subsections in a section. 我想做的是遍历一个小节中的一部分。 When I run my code I get the error, "TypeError: 'Section' object is not callable". 当我运行代码时,出现错误“ TypeError:'Section'对象不可调用”。

Here's my code: 这是我的代码:

section = webconf['Items']
for subsection in section(range(1,3)):
   <code block>

What I'm trying to do is to avoid iterating over the 'DEFAULTS' subsection for that section. 我想做的是避免重复该部分的'DEFAULTS'小节。 I suspect this is failing because the section is a dict whereas I've tried referencing it as a list. 我怀疑这是失败的,因为该部分是字典,而我尝试将其作为列表引用。 However, I am not sure how to cleanly achieve my goal when the object is a "dict" (or configObj.Section). 但是,我不确定当对象是“ dict”(或configObj.Section)时如何干净地实现我的目标。

If someone can put me on the right track I'll greatly appreciate it:) 如果有人可以让我走上正确的道路,我将不胜感激:)

Try 尝试

for subsectionKey in section.keys():
    if subsectionKey != 'default':
        # - do your code here
        # - subsectionKey is just a key (a string), so
        #   make use of it with section[subsectionKey]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM