printing rows and columns from a xml file in python using the range function in lenny

In your code selectedrows just holds three tags for each iteration of the for x loop. If I understand this correctly, you want to initialize the list selectedrows before the 1for xloop, append the three tags as one element of this list, and once you parsed your xml, print a subset ofselectedrows`. so something like this

selectedrows = []
for x in root.findall('root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows.append([tag1, tag4, tag5])

# this will run after you have gone through your xml doc with selectedrows populated
for i in range(0, len(selectedrows), 2):
    print(selectedrows[i])

the above will print every other row (not stopping at row 20). You can modify it to for i in range(0, 19, 2) if you want. Also, if top 20 rows is all you want, you may want to stop the for x loop after 20 iterations

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top