Вот пример, в котором я пытаюсь вставить атрибут attr2 перед атрибутом attr3:
Код: Выделить всё
from lxml import etree
xml = ''
root = etree.fromstring(xml)
inse_pos = root.keys().index('attr3')
attrib_items = root.items()
attrib_items.insert(inse_pos, ('attr2', 'val2'))
root.attrib = dict(attrib_items)
print(etree.tostring(root))
Подробнее здесь: https://stackoverflow.com/questions/780 ... n-position
Мобильная версия