Python3六 - AttributeError:自定义对象没有属性'items'

时间:2016-12-01 19:26:27

标签: python python-3.x six

我有dict Casedict类型的对象列表,这些对象以six启动。 在Python2中运行时,我可以遍历键和值,以方便我使用class Case: def __init__(self, obj): properties = {} # standardize Boolean properties true_values = ['1', 1, True, 'yes', 'Yes', 'YES'] false_values = ['0', 0, False, 'no', 'No', 'NO'] for (k, v) in iteritems(properties): # standardize TRUE values if v in true_values: properties[k] = True # standardize FALSE values elif v in false_values: properties[k] = False # keep String values elif isinstance(v, str): continue # remove NULL values filtered = {k: v for (k, v) in iteritems(properties) if v != None} # set Case instance attribute for each property for (k, v) in iteritems(filtered): setattr(self, k, v) def __iter__(self): return self def __getitem__(self, key): return self.__dict__.get(key, None) def to_json(self): return json.dumps(self, default=lambda o: o.__dict__) 库的两个Python版本。

six

当我遍历此列表时,尽管遵循from six import iteritems def transform(case_list): for case in case_list: for (key, value) in iteritems(case): #crashes here #do stuff documentation,我仍会收到AttributeError。

我遍历上面的对象列表:

Traceback (most recent call last):
File "app/run.py", line 134, in <module>
main()
File "/home/user/other.py", line 55, in transform
for (key, value) in iteritems(case):
File "/home/user/env/lib/python3.4/site-packages/six-1.10.0-py3.4.egg/six.py", line 581, in iteritems
return iter(d.items(**kw))
AttributeError: 'Case' object has no attribute 'items'

但每次都会得到一个AttributeError:

python3 app/run.py

我用EntityResolver<String> orderNumberResolver = new EntityResolver<String>() { @Override public String resolve(String partitionKey, String rowKey, Date timeStamp, HashMap<String, EntityProperty> properties, String etag) { return properties.get("SomeColumnName").getValueAsString(); } }; 来称呼它。为什么它不能识别可迭代?

1 个答案:

答案 0 :(得分:0)

我通过向Case类添加items()方法找到了解决方案:

    def items(self):
        return zip(self.__dict__.values(), self.__dict__.keys())