Skip to content
Advertisement

Printing rows and columns from xml file in python using range function with len()

I have over 1000 rows imported via an xml file in python and I want to print every second row, starting at the first, to the 20th row from three selected tabs/columns but I can’t figure out how to put the range function into the print function of an xml file. See sample code below:

##importing xml file using element tree

import xml.etree.ElementTree as ET

xmlFile = "FilePath/DataName.xml"
tree = ET.parse(xmlFile)
root = tree.getroot()

try: import xml.etree.ElementTree as ET
except ImportError:
    print('An error occurred trying to read the file.')

print(root[0].tag)

##example 1##This prints everything from selected tags/columns###

for x in root.findall('Root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows = [tag1, tag4, tag5]
    for i in range(0, len(selectedrows)):
        print(selectedrows[i])

##example 2##I can print every second row but can’t put in the max. And if I put in (0, 19, 2) as the range with len() I get an error.

print(root[0].tag)

for x in root.findall('root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows = [tag1, tag4, tag5]
    for i in range(0, len(selectedrows), 2):
        print(selectedrows[i])

##The above code prints the selected columns/tags but how do I include range(0, 19, 2) into the range function above. I’ve gone through a number of tutorials online printing text using the range(0, len(??)) function but none gives detail on how to select rows and columns when printing. Is it even possible to do this? Or should I save as CSV and create arrays?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement