我有代码:
from lxml import etree
root = etree.Element("check", attrib={"p": "1","c": "2", "d": "3","v": "4"})
tree = etree.ElementTree(root)
我得到了:
<check c="2" d="3" p="1" v="4"/>
但我需要没有属性排序:
<check p="1" c="2" d="3" v="4"/>
我怎么能得到它?
非常感谢您的帮助。
答案 0 :(得分:0)
你无法控制某种属性。但您可以访问属性:
tree = etree.ElementTree(root)
att = tree.attrib
print (att) # {attr_name : attr_value}
来自“attr”的你只能得到attr_name:
attr_name = att.kyes() # ['attr_name_1', 'attr_name_2'...]