Python trouble with looping through dictionary and replacing xml attribute
<p>I am having trouble looping through Python list below and replacing xml attribute with all <strong>ADSType</strong> values. </p>

<p><strong>Python Dictionary</strong></p>

<pre><code>{'ADSType': ['HS11N', 'HS11V'], 'Type': ['Bond', 'Cash']}
</code></pre>

<p><strong>XML</strong></p>

<p>I'd like to replace <strong>sid</strong> values on each line in XML with ADS values. </p>

<pre><code>&lt;req Times="1" action="get" chunklimit="1000" lang="ENU" msg="1" rank="1" rnklst="1" runuf="0"&gt;
&lt;flds&gt;
        &lt;f end="2016-02-29" freq="m" i="bond(long) hff" sid="abc" start="2016-02-29" /&gt;
        &lt;f end="2016-02-29" freq="m" i="bond(short) ggg" sid="abc" start="2016-02-29" /&gt;
&lt;/flds&gt;
&lt;dat&gt;
        &lt;r CalculationType="3" ForceCalculate="1" i="123" /&gt;
&lt;/dat&gt;
&lt;/req&gt;
</code></pre>

<p><strong>Python Code so far:</strong></p>

<pre><code>data_list = {'ADSType': ['HS11N', 'HS11V'], 'Type': ['Bond', 'Cash']}

xml_file = './test.xml'
tree1 = ET.ElementTree(file=xml_file)
root1 = tree1.getroot()

for x in root1.iter('flds'):
    for y in x.iter('f'):
        y.set('sid', y.get('sid').replace("123", "abc"))
# print(ET.tostring(root1).decode("UTF-8"))
    print "test"
# print(ET.tostring(root1).decode("UTF-8"))
tree1.write("./test.xml")
</code></pre>

<p>I am stuck how to iterate over list, extract values, feed in replace method dynamically and update xml. Please help! Following is the desired output I am trying to achieve.</p>

<pre><code>&lt;req Times="1" action="get" chunklimit="1000" lang="ENU" msg="1" rank="1" rnklst="1" runuf="0"&gt;
&lt;flds&gt;
        &lt;f end="2016-02-29" freq="m" i="bond(long) hff" sid="HS11N" start="2016-02-29" /&gt;
        &lt;f end="2016-02-29" freq="m" i="bond(short) ggg" sid="HS11V" start="2016-02-29" /&gt;
&lt;/flds&gt;
&lt;dat&gt;
        &lt;r CalculationType="3" ForceCalculate="1" i="123" /&gt;
&lt;/dat&gt;
&lt;/req&gt;
</code></pre>

<p>This is a quick and dirty way of doing it.</p>

<pre><code>for x in root1.iter('flds'):
    f = x.iter('f')
    for idx,y in enumerate(x):
        if y &lt; len(f):
            y.set('sid', y.get('sid').replace("abc",data_list['ADSType'][y]))
</code></pre>

<p>Simply use enumerate to get the index of f within XML. This is then used for the index within the data_list ADSType.</p>

<p>Not added any verification of XML to make sure the attribute to replace exists. Nor have I took into consideration multiple 'flds' tags. Writing on a tablet so awkward to add more robust code. I'll try and update with something less quick and dirty when back at laptop</p>

python xml loops dictionary