input:
Код: Выделить всё
Fiction
4
300
Fictional Fiction
2
500
Horrors
1
450
Horrendous Horror
3
20
Comedic Comedy
0
1
< /code>
Я хочу организовать все элементы жанров по своему дочернему элементу «ID». Это вывод, на который я собираюсь:
[b] output: [/b]
Comedic Comedy
0
1
Horrors
1
450
Fictional Fiction
2
500
Horrendous Horror
3
20
Fiction
4
300
< /code>
Это код, который я пробовал: < /p>
def sortchildrenby(parent):
parent[:] = sorted(parent, key=lambda child: child.tag == 'id')
filename = "Example.xml"
tree = ET.parse(filename)
root = tree.getroot()
attr = "type"
for elements in root:
sortchildrenby(elements)
tree.write("exampleORGANIZED.xml")
< /code>
, который приводит к следующему xml: < /pbr />
Fiction
300
4
Fictional Fiction
500
2
Horrors
450
1
Horrendous Horror
20
3
Comedic Comedy
1
0
Подробнее здесь: https://stackoverflow.com/questions/715 ... -in-python