AttributeError:“列表”对象没有属性“ iter”

时间:2019-07-09 23:59:20

标签: xml python-3.x serialization

通过et.tostring()返回列表时,此代码生成以下错误“ AttributeError:'list'对象没有属性'iter'”。我将xml.etree.ElementTree导入为et

我尝试使用et.tostringlist(),但在其他地方找不到解决我问题的方法。

import xml.etree.ElementTree as et
class SerializerFactory:

    def serialize_all(self, format, member_list):
        if format == 'JSON':
            serialize = JsonSerializer(member_list)
            serialize.start_all_objects()
            return serialize
        elif format == 'XML':
            serialize = XmlSerializer(member_list)
            serialize.start_all_objects()
            return serialize
        else:
            raise ValueError("Format must be 'JSON' or 'XML'.")

    def serialize_one(self, format, index, member_list):
        if format == 'JSON':
            return JsonSerializer(member_list).start_one_object(index)
        elif format == 'XML':
            return XmlSerializer(member_list).start_one_object(index)
        else:
            raise ValueError("Format must be 'JSON' or 'XML'.")


class XmlSerializer:
    def __init__(self, member_list):
        self.member_list = member_list
        self.serialize_list = [] * member_list.size

    def start_all_objects(self):
        for i in range(self.member_list.size):
            member = self.member_list[i]
            number = member.mem_num
            l_name = member.l_name
            f_name = member.f_name
            mem_type = member.mem_type
            mem_list = et.Element({'Number': number, 'Last Name': l_name,
                                   'First Name': f_name, 'Membership Type': mem_type})

            self.serialize_list.append(mem_list)

    def start_one_object(self, index):
        member = self.member_list[index]
        number = member.mem_num
        l_name = member.l_name
        f_name = member.f_name
        mem_type = member.mem_type
        mem_list = et.Element({'Number': number, 'Last Name': l_name,
                               'First Name': f_name, 'Membership Type': mem_type})
        self.serialize_list.append(mem_list)

    def to_str(self):
        return et.tostring(self.serialize_list, encoding='unicode')


factory = MemberSerializer.SerializerFactory()
xml = factory.serialize_all('XML', mem_list)
print(xml.to_str())

我想返回xml中的整个列表,不确定是否确实可行,但这似乎是合理的。这是我收到的错误消息

Traceback (most recent call last):
    File "C:/Users/Nate Dawg/PycharmProjects/Membership/Test.py", line 36, in <module>
    print(xml.to_str())
File "C:\Users\Nate Dawg\PycharmProjects\Membership\MemberSerializer.py", line 89, in to_str
    return et.tostring(self.serialize_list, encoding='unicode')
File "C:\Users\Nate Dawg\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1135, in tostring
    short_empty_elements=short_empty_elements)
File "C:\Users\Nate Dawg\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 773, in write
    qnames, namespaces = _namespaces(self._root, default_namespace)
File "C:\Users\Nate Dawg\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 876, in _namespaces
    for elem in elem.iter():
AttributeError: 'list' object has no attribute 'iter'

0 个答案:

没有答案