0

I have xml:

<?xml version="1.0" encoding="UTF-8"?>
<rows>
  <row>
    <ro new="TEMP_1">TEMP_11</ro>
    <ro new="TEMP_2">TEMP_12</ro>
   <ro new="TEMP_3">TEMP_13</ro>
  </row>
 <row>
    <ro new="TEMP_1">TEMP_14</ro>
    <ro new="TEMP_2">TEMP_15</ro>
    <ro new="TEMP_3">TEMP_16</ro>
  </row>
 </rows>

and parser:

import xml.etree.cElementTree as ET

context = ET.iterparse('temp.xml', events=("start", "end"))
context = iter(context)

outList = []
for event,elem in context:
    tag = elem.tag
    value = elem.text
    outList.append(value)
print outList

when print outList I recive :

 ['\n', '\n', 'TEMP_11', 'TEMP_11', 'TEMP_12', 'TEMP_12', 'TEMP_13', 'TEMP_13', '\n', '\n', 'TEMP_14', 'TEMP_14', 'TEMP_15', 'TEMP_15', 'TEMP_16', 'TEMP_16', '\n', '\n']

Why I receives duplicate values in list? How to fix it?

2 Answers 2

2

You have duplicates because you're appending twice -- on both the start events and the end events.

Either listen to only one event type or the other, or inspect the event type as you iterate. To do the former is a one-line change:

context = ET.iterparse('temp.xml', events=('end',))

...or, if you want to listen to both event types for some other reason:

for event, elem in context:
    if event == 'end':
        outList.append(elem.text)
Sign up to request clarification or add additional context in comments.

Comments

0

Because the start and end event happen for the same tag. Also, what's the point of doing this:

context = iter(context)

context is already an iterator because iterparse() returns an iterator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.