Reputation: 264
I am trying to convert an xml file into a csv file. Below is what i have tried so far:
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():
image_path = os.path.join(os.getcwd(), "/examples/xmlfiles")
xml_df = xml_to_csv(image_path)
xml_df.to_csv('1.csv', index=None)
print('Successfully converted xml to csv.')
main()
i have kept my xml file naming " 1.xml" in "currdir/examples/xmlfiles" and my python script is in "currdir". when i run this script it displays "Successfully converted xml to csv" but i see no such file named 1.csv anywhere. what am i doing wrong in this. Thanks in advance.
Upvotes: 1
Views: 1891
Reputation: 452
i suspect its not able to find the xml files or not able to convert them, put print statements in the code and validate that
Based on the code the file 1.csv should sit in current directory where you are invoking the python script from. However you can specify a different location in the command and save it there as well like below...Find out way to specific exact relative path on your OS its either "/examples/xmlfiles" or just "examples/xmlfiles" xml_df.to_csv('examples/xmlfiles/1.csv', index=None)
Upvotes: 1