Skip to content Skip to sidebar Skip to footer

Why Do I Keep Getting Child Out Of Range Error?

I am using this python script to convert xml's to csv: import os import glob import pandas as pd import xml.etree.ElementTree as ET def xml_to_csv(path): xml_list = [] f

Solution 1:

According to sample XML, the bundbox element is located at node 5, not 4. By the way, there's no need to use pandas for csv migration. Consider the csv (built-in module as of Python 3). Also below shows you can use numbered indexing, [##], or find() for the nodes: xmin, ymin, xmin, and xmax.

# ALL STANDARD LIBRARY MODULES
import os, glob, csv
import xml.etree.ElementTree as ET

def xml_to_csv(path):

    with open("Output.csv", "w") as f:
        cw = csv.writer(f, lineterminator="\n")
        cw.writerow(['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'])

        for xml_file in glob.glob(path + '/*.xml'):
            tree = ET.parse(xml_file)
            root = tree.getroot()

            for member in root.findall('object'):
                value = (root.find('filename').text,
                         int(root.find('size')[0].text),
                         int(root.find('size')[1].text),
                         member[0].text,
                         int(member[5][0].text),
                         int(member[5][1].text),
                         int(member[5].find('ymin').text),
                         int(member[5].find('ymax').text)
                        )
                cw.writerow(value)

Post a Comment for "Why Do I Keep Getting Child Out Of Range Error?"