replace xml child element text with value from a list python
<p>I want to replace the text element of a child with a value held in a list. </p>

<pre><code>&lt;weighting name="weighting"&gt;
        &lt;aWeight&gt;false&lt;/aWeight&gt;
        &lt;cWeight&gt;true&lt;/cWeight&gt;
&lt;/weighting&gt;
</code></pre>

<p>I am trying to change the text value for aWeight to true. I have tried to do it with this code.</p>

<pre><code>elems = dom.findall('aWeight')
for elem in elems:
    if user_settings.new_settings[5] == 'true':
        '&lt;aWeight&gt;'.text = 'true'

dom.write('output.xml')
</code></pre>

<p>It's writing the file but the value is still staying at false. Does anyone have any suggestions.</p>

<p>As your code stands it's not accessing the <code>elem</code> variable to change the node value, you need to tell the operation "where" it needs to operate.</p>

<pre><code>import xml.etree.ElementTree as ET
elems = ET.fromstring('&lt;weighting&gt;&lt;aWeight&gt;false&lt;/aWeight&gt;&lt;cWeight&gt;true&lt;/cWeight&gt;&lt;/weighting&gt;')

test_condition = ['false', 'not set', 'bananas']

elems2 = elems.findall("aWeight")
for elem in elems2:
    print elem.text
    if elem.text in test_condition: 
        elem.text = 'true' 
    print elem.text
print ET.tostring(elems)
</code></pre>

python xml list