无法迭代python字典

时间:2015-04-22 09:13:46

标签: python arguments attributeerror

我尝试将一个字典传递给一个将“解析”它的函数,但我不能迭代它,它看起来很简单,但无法理解发生了什么。

我的功能:

    def _build_options(self, options):
        print options
        for option, value in options.iteritems():
            if option not in self._optionslist:
                del options[option]
        return options

选项:

 {'segment_container': None, 'use_slo': False, 'log_level': 'INFO', 
'dir_marker': 'False', 'changed': None, 'leave_segments': False, 'fail_fast':
 False, 'headers': 'X-Delete-After:50, X-Upload:None', 'meta': [], 
'ttl': None, 'segment_size': None, 'skip_identical': False}

我的代码提出了这个问题:

AttributeError: Values instance has no attribute 'iteritems'

由于

编辑: 类型(选项):

 <type 'instance'>

DIR(选项):

['__cmp__', '__doc__', '__init__', '__module__', '__repr__', '__str__',
 '_update', '_update_careful', '_update_loose', 'changed', 'dir_marker',
'ensure_value', 'fail_fast', 'headers', 'leave_segments', 'log_level', 'meta', 'read_file', 'read_module', 
'segment_container', 'segment_size', 'skip_identical', 'ttl', 'use_slo']

我尝试了options = dict(options),但是我收到了错误:

    TypeError: iteration over non-sequence

EDIT2: 我有一个方法upload(),我在其中调用_build_options():

def upload(self, paths, ttl, options):
     # [...]
     # print options, type(options)
     # the dictionnary is well printed but its type is "instance"
     opts = self._build_options(options)

upload()在另一个文件中调用:

self.swift_mng.upload(filename, None, options)

我从optparse.parse_args()

中获取选项

1 个答案:

答案 0 :(得分:2)

不确定属性错误。但是当你在它上面循环时,不能更改字典大小。如评论中所述,您可以检查选项的类型,并执行以下操作:

kartik