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>You don't need to replace anything, just use <em>set</em> with the new value which will overwrite:</p>

<pre><code>from xml.etree import ElementTree as ET
xml_file = "./test.xml"

tree = ET.parse(xml_file)
root1 = tree.getroot()
data_list = {'ADSType':['HS11N', 'HS11V'], 'Type': ['Bond', 'Cash']}

# make iterator so we just call next(sids) to pull each value
sids = iter(data_list['ADSType'])

for flds in root1.iter('flds'):
    for f in flds.iter('f'):
        # set to new value
        f.set("sid", next(sids))
# write
tree.write("./test.xml")
</code></pre>

<p>That does exactly what you want although I am not sure how the full dict fits into your question:</p>

<p>test.xml:</p>

<pre><code>In [3]: !cat test.xml
&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>Run the code:</p>

<pre><code>In [4]: from xml.etree import ElementTree as ET

In [5]: xml_file = "./test.xml"

In [6]: tree = ET.parse(xml_file)

In [7]: root1 = tree.getroot()

In [8]: data_list = {'ADSType': iter(['HS11N', 'HS11V']), 'Type': iter(['Bond', 'Cash'])}

In [9]: sids = iter(data_list['ADSType'])

In [10]: for flds in root1.iter('flds'):
   ....:     for f in x.iter('f'):
   ....:              f.set("sid", next(sids))
   ....: tree.write("./test.xml")
   ....: 
</code></pre>

<p>New test.xml:</p>

<pre><code>In [11]: !cat test.xml
&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>If you have multiple f's inside multiple fld's and want to cycle the values, use <em>itertools.cycle</em> in place of iter:</p>

<pre><code>from itertools import cycle
sids = cycle(data_list['ADSType'])
</code></pre>

python xml loops dictionary