0

I have code:

from lxml import etree
root = etree.Element("check", attrib={"p": "1","c": "2", "d": "3","v": "4"})
tree = etree.ElementTree(root)

and I get:

<check c="2" d="3" p="1" v="4"/>

But i need without attribute sorting:

<check p="1" c="2" d="3" v="4"/>

How i can get it?

Many thanks for any help.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Ann
  • 95
  • 2
  • 7
  • You can use OrderedDict to preserve attribute order. See https://stackoverflow.com/a/22596064/407651. – mzjn Jun 11 '18 at 09:02

1 Answers1

0

you cannot control sort of attribute. but you can have access to attribute:

tree = etree.ElementTree(root)
att = tree.attrib
print (att) # {attr_name : attr_value}

from "attr" you can get only attr_name:

attr_name = att.kyes() # ['attr_name_1', 'attr_name_2'...]