How to order xml element attributes in Python?

When parsing an xml file in Python ElementTree, the order of the attributes is mixed because Python stores the attributes in a dictionary.

How to change the order of attributes in a dictionary?

+2
source share
4 answers

Your answer to yourself, as you said, is long and cumbersome. It should not be. It will also fail if (1) has more than 10 keys (2), the dict has fewer keys than expected.

Try it; it is much simpler:

>>> ordered_keys = ('z', 'y', 'e', 'x', 'w') # possible keys, in desired order

Note: the line above is all the necessary settings.

>>> dic = {'z':'a', 'y':'b', 'x':'c', 'w':'d'} # actual contents of a dictionary
>>> for k in ordered_keys:
...     if k in dic: # avoid trouble if a key is missing
...         print k, dic[k]
...
z a
y b
x c
w d
>>>
+1
source

XML- 1 3.1 .

1 , , , .

+6

XML node. , . , . , - . .

0

. , .

, , , , / , .

0
source

Source: https://habr.com/ru/post/1545695/


All Articles