how to convert from csv to xml with python

Question:

How to convert this csv format[‘x’,’y’,’width’,’height’,’tag’]

enter image description here

to this XML format using python script?

<annotation>
    <object>
        <tag>figure</tag>
        <bndbox>
            <x>1.0</x>
            <y>100.0</y>
            <width>303.0</width>
            <height>619.0</height>
        </bndbox>
    </object>
    <object>
        <tag>text</tag>
        <bndbox>
            <x>338.0</x>
            <y>162.0</y>
            <width>143.0</width>
            <height>423.0</height>
        </bndbox>
    </object>
    <object>
        <tag>text</tag>
        <bndbox>
            <x>85.0</x>
            <y>768.0</y>
            <width>554.0</width>
            <height>39.0</height>
        </bndbox>
    </object>
</annotation>

Note this is for the first row of the csv file i want to convert for all row

Asked By: myomin htet

||

Answers:

You can use python Panda for this
https://pypi.org/project/pandas/

imports pandas as pd
df= pd.read_csv('movies2.csv')
with open('outputf.xml', 'w') as myfile: 
myfile.write(df.to_xml())
Answered By: Hari

make sure you have titles in your csv .

import pandas as pd
def convert_row(row):
    return """<annotation>
    <object>
        <tag>%s</tag>
        <bndbox>
            <x>%s</x>
            <y>%s</y>
            <width>%s</width>
            <height>%s</height>
        </bndbox>
    </object>""" % (
    row.tag, row.x, row.y, row.width, row.hight)

df = pd.read_csv('untitled.txt', sep=',')
print 'n'.join(df.apply(convert_row, axis=1))
Answered By: Ariel Tarayants

Good morning.
I was interested in reading / writting the csv files with Python.
This is, because I wasn’t able to read a file on my own computer.
I’m not talking more about other files, because I have no idea.

Personally I had tried the IDLE Pyhon editor.
This can also run and debug Python applications.
If you know another Python editor, it can be also another choice.

It’s not necessary to use a different application for a certain task.
The python program can read your csv file and writes another xml file. I didn’t thought what happens, if the csv file has a title row.

I had created a Py_2_xml.py file.
This can be placed in the same folder as your movies2.csv
First time you can create the file as a text file wih Notepad.
Rename the extension and then edit with the installed IDLE editor.
Here is my code:

import csv
csvfile=open('./movies2.csv')
reader=csv.reader(csvfile)
data="<annotation>n"
for line in reader: 
    #print(line)
    data = data + ' <object>n'
    data = data + '     <tag>' + line[4] + '</tag>n'
    data = data + '     <bndbox>n'
    i = 0
    for item in line: 
        if i == 0:
            data = data + '         <x>' + item + '</x>n'
        elif i == 1:
            data = data + '         <y>' + item + '</y>n'
        elif i == 2:
            data = data + '         <width>' + item + '</width>n'
        elif i == 3:
            data = data + '         <height>' + item + '</height>n'
        i = i + 1
        #print(item)
    data = data + '     </bndbox>n'
    data = data + ' </object>n'
csvfile.close()
data = data + '</annotation>n'
xmlfile=open('outputf.xml','w')
xmlfile.write(data)
xmlfile.close()
print(data)
data = ''

#import subprocess
#subprocess.Popen(["notepad","outputf.xml"])

I’m sorry, first time I missed to write the object tag.
Then the last column was not properly read.

The items in the line object being read by csv reader, can be referenced also as items of an array.
(So that the items in the columns could be read in any order).

This can simplify more the code.
Instead of parsing all items in the line array, do like this:

import csv
csvfile=open('./movies2.csv')
reader=csv.reader(csvfile)
data="<annotation>n"
for line in reader: 
    #print(line)
    data = data + ' <object>n'
    data = data + '     <tag>' + line[4] + '</tag>n'
    data = data + '     <bndbox>n'
    data = data + '         <x>' + line[0] + '</x>n'
    data = data + '         <y>' + line[1] + '</y>n'
    data = data + '         <width>' + line[2] + '</width>n'
    data = data + '         <height>' + line[3] + '</height>n'
    data = data + '     </bndbox>n'
    data = data + ' </object>n'
csvfile.close()
data = data + '</annotation>n'
xmlfile=open('outputf.xml','w')
xmlfile.write(data)
xmlfile.close()
print(data)
data = ''

Preferably the file won’t have to be opened in an application.
Use import os libraries to copy the file into another location.
But first it should be aquired the destination path.
Some people use the Tkinkter library to show a SaveDialog.

Be careful at each line indentation in the Python code.
Because when a tab is wrong, it will result an error.
This is really working for me.
Best regards, Adrian Brinas.

Answered By: Adrian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.