Skip to main content
 首页 » 编程设计

python之对 configObj 部分的选择性迭代

2025年05月04日13bjzhanghao

我在迭代 configObj 对象中的部分时遇到一些问题。我想要做的是迭代一个部分中的子部分。当我运行我的代码时,出现错误“TypeError:'Section' object is not callable”。

这是我的代码:

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

我想做的是避免迭代该部分的“默认”子部分。我怀疑这是失败的,因为该部分是一个字典,而我已经尝试将其作为列表引用。但是,当对象是“dict”(或 configObj.Section)时,我不确定如何干净地实现我的目标。

如果有人能让我走上正轨,我将不胜感激:)

请您参考如下方法:

尝试

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]