Convert XML-file to CSV

4.9k Views Asked by At

I'm struggling with the following case. I've a XML file in the following format:

<event>
  <attribute type="NAME">John</attribute>
  <attribute type="TASK">Buy</attribute>
  <attribute type="DATE">12052017</attribute>
</event>
<event>
  <attribute type="NAME">John</attribute>
  <attribute type="RESOURCE">Dollar</attribute>
  <attribute type="DATE">13052017</attribute>
</event>

I need to transform it into a CSV file. The outcome should be:

John,Buy,,12052017
John,,Dollar,13052017

I'm using a small Python script I wrote for Notepad++ that searches and deletes everything that shouldn't be in the string. For example:

editor.rereplace('\r\n  <attribute type="NAME">', '');

This works fine, but it messes up the sequence of attribute (since if it doesn't find <attribute type="TASK"> it doesn't places an extra ,. The outcome then is:

John,Buy,12052017
John,Dollar,13052017

Making no difference between the attribute TASK and RESOURCE.

I've checked different topics but none really covered my question. Can somehelp me with a cheap trick or point me to a tool.

2

There are 2 best solutions below

0
On

For my project I am using this python script:

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 directory in ['train','test']:
        image_path = os.path.join(os.getcwd(), 'images/{}'.format(directory))
        xml_df = xml_to_csv(image_path)
        xml_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
        print('Successfully converted xml to csv.')


main()
1
On

The data needs to be a valid xml document

data = '''<?xml version="1.0"?>
<data>
<event>
  <attribute type="NAME">John</attribute>
  <attribute type="TASK">Buy</attribute>
  <attribute type="DATE">12052017</attribute>
</event>
<event>
  <attribute type="NAME">John</attribute>
  <attribute type="RESOURCE">Dollar</attribute>
  <attribute type="DATE">13052017</attribute>
</event>
</data>
'''

you can do somenthing like this to extract what you need

import xml.etree.ElementTree as ET

doc = ET.fromstring(data)

mycsv = []


for event in doc:
    row = {}
    for attr in event:
        if attr.tag == 'attribute':
            print attr.tag, attr.attrib, attr.text
            row[attr.attrib['type']] = attr.text
    mycsv.append(row)

and the result will be:

[{'DATE': '12052017', 'TASK': 'Buy', 'NAME': 'John'}, {'DATE': '13052017', 'RESOURCE': 'Dollar', 'NAME': 'John'}]

and writing into csv file

import csv

keys = ['NAME', 'TASK', 'RESOURCE', 'DATE']
with open('result.csv', 'wb') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(mycsv)