Reputation: 46
Python 2 Is it posible to change xml file using python when
<Label name="qotl_type_label" position="910,980" font="headline_light" />
Search by name attribute and then change position?
Upvotes: 0
Views: 439
Reputation: 474271
You can use the built-in xml.etree.ElementTree
module to parse the XML, locate the Label
element and change the position
attribute via .attrib
property:
>>> import xml.etree.ElementTree as ET
>>>
>>> s = '<root><Label name="qotl_type_label" position="910,980" font="headline_light" /></root>'
>>>
>>> root = ET.fromstring(s)
>>> label = root.find(".//Label[@name='qotl_type_label']")
>>> label.attrib['position'] = 'new,position'
>>> ET.tostring(root)
'<root><Label font="headline_light" name="qotl_type_label" position="new,position" /></root>'
Note that the order of attributes is not preserved, attributes are unordered by definition.
Upvotes: 1