Answer the question
In order to leave comments, you need to log in
How to convert XML files to CSV files?
Trying to convert all XML files to CSV files. For this I use python xml_to_csv.py
xml_to_csv.py itself looks like this:
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = []
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[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def main():
for folder in ['train','test']:
image_path = os.path.join(os.getcwd(), ('images/' + folder))
xml_df = xml_to_csv(image_path)
xml_df.to_csv(('images/' + folder + '_labels.csv'), index=None)
print('Successfully converted xml to csv.')
main()
Answer the question
In order to leave comments, you need to log in
Everything worked out correctly. You have created a comma delimited csv file. If you want to open csv and have everything in columns, use a semicolon as a delimiter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question