Python change xml attribute
<p>Python 2
Is it posible to change xml file using python when</p>

<pre><code>&lt;Label name="qotl_type_label" position="910,980" font="headline_light" /&gt;
</code></pre>

<p>Search by name attribute and then change position?</p>

<p>You can use the built-in <a href="https://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow"><code>xml.etree.ElementTree</code></a> module to parse the XML, locate the <code>Label</code> element and change the <code>position</code> attribute via <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.attrib" rel="nofollow"><code>.attrib</code></a> property:</p>

<pre><code>&gt;&gt;&gt; import xml.etree.ElementTree as ET
&gt;&gt;&gt;
&gt;&gt;&gt; s = '&lt;root&gt;&lt;Label name="qotl_type_label" position="910,980" font="headline_light" /&gt;&lt;/root&gt;'
&gt;&gt;&gt;
&gt;&gt;&gt; root = ET.fromstring(s)
&gt;&gt;&gt; label = root.find(".//Label[@name='qotl_type_label']")
&gt;&gt;&gt; label.attrib['position'] = 'new,position'
&gt;&gt;&gt; ET.tostring(root)
'&lt;root&gt;&lt;Label font="headline_light" name="qotl_type_label" position="new,position" /&gt;&lt;/root&gt;'
</code></pre>

<p>Note that the order of attributes is not preserved, attributes are unordered <em>by definition</em>.</p>

python python-2.7