<user internal-id="1233232323">
<name>Alex</name>
<age>23</age>
<image id="1">http://example.com/images/img1.jpg</image>
<image id="2">http://example.com/images/img2.jpg</image>
<image id="3">http://example.com/images/img3.jpg</image>
<image id="4">http://example.com/images/img4.jpg</image>
<image id="5">http://example.com/images/img5.jpg</image>
</user>
<user internal-id="24323423423">
<name>Daniel</name>
<age>31</age>
<image id="1">http://example.com/images/img1.jpg</image>
<image id="2">http://example.com/images/img2.jpg</image>
<image id="3">http://example.com/images/img3.jpg</image>
<image id="4">http://example.com/images/img4.jpg</image>
<image id="5">http://example.com/images/img5.jpg</image>
</user>
и т.д.
root = etree.fromstring(data)
users = []
for user in root.getchildren():
print user.get('internal-id')
#а здесь нужно перебрать и вывести все относящиеся к этому юзеру картинки.
#Не понимаю, как это сделать? Подскажите, пожалуйста.</b>
import xml.etree.cElementTree as ET
from typing import List
from typing import NamedTuple
from typing import Tuple
class User(NamedTuple):
internal_id: int
name: str
age: int
images: Tuple[str]
XML: str = """
<root>
<user internal-id="1233232323">
<name>Alex</name>
<age>23</age>
<image id="1">http://example.com/images/img1.jpg</image>
<image id="2">http://example.com/images/img2.jpg</image>
<image id="3">http://example.com/images/img3.jpg</image>
<image id="4">http://example.com/images/img4.jpg</image>
<image id="5">http://example.com/images/img5.jpg</image>
</user>
<user internal-id="24323423423">
<name>Daniel</name>
<age>31</age>
<image id="1">http://example.com/images/img1.jpg</image>
<image id="2">http://example.com/images/img2.jpg</image>
<image id="3">http://example.com/images/img3.jpg</image>
<image id="4">http://example.com/images/img4.jpg</image>
<image id="5">http://example.com/images/img5.jpg</image>
</user>
</root>
"""
if __name__ == '__main__':
users: List[User] = []
root = ET.fromstring(XML)
for user in root:
user_data = tuple(tag.text for tag in user)
users.append(
User(
internal_id=user.get('internal-id'),
name=user_data[0],
age=user_data[1],
images=user_data[2:]
)
)