How to convert CSV to XML using Python

Introduction

Here I am going to create an example on how to convert CSV to XML. CSV means Comma Separated Value. So the sources of these values may be different. These values may be put into a file or in a string.

XML is an Extensible Markup Language that defines a set of rules for encoding document in a format that is both human and machine readable.

Here I am going to convert either CSV string or file to XML. The output can be written either in XML string or file.

Comma Separated Values (CSV) and the Extensible Markup Language (XML) are the most widely used formats for data, and conversion between these two formats needs often to be accomplished.

Especially to XML, because this format is very well supported by modern applications, and is very well suited for further data manipulation and customization.

Prerequisites

Python 3.8.3 – 3.9.1

Convert CSV to XML

Now I will write the code in Python programming language to convert csv data to xml data.

Before I convert into XML data I need to read the CSV data. There are various ways to read CSV data either from file or csv string.

Here I am going to use simple way to read the csv data and produce the xml output. Here I am reading from csv file, you can also read from csv string. You can check link various ways to read CSV data either from file or csv string.

The output in displayed in console.

The sample csv file can be downloaded later from the source code.

import csv

f = open('sample.csv')
csv_f = csv.reader(f)   
data = []

for row in csv_f: 
	data.append(row)
f.close()

#print (data[1:])

def convert_row(row):
    return """<policy>
	<policyId>%s</policyId>
    <statecode>%s</statecode>
    <eq_site_limit>%s</eq_site_limit>
    <hu_site_limit>%s</hu_site_limit>
    <fl_site_limit>%s</fl_site_limit>
    <fr_site_limit>%s</fr_site_limit>
    <tiv_2011>%s</tiv_2011>
	<tiv_2012>%s</tiv_2012>
	<eq_site_deductible>%s</eq_site_deductible>
	<hu_site_deductible>%s</hu_site_deductible>
	<fl_site_deductible>%s</fl_site_deductible>
	<fr_site_deductible>%s</fr_site_deductible>
	<point_latitude>%s</point_latitude>
	<point_longitude>%s</point_longitude>
	<line>%s</line>
	<construction>%s</construction>
	<point_granularity>%s</point_granularity>
</policy>""" % (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16])

print ('\n'.join([convert_row(row) for row in data[1:]]))

The above code will give you the following output:

<policy>
	<policyId>119736</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>498960</hu_site_limit>
    <fl_site_limit>498960</fl_site_limit>
    <fr_site_limit>498960</fr_site_limit>
    <tiv_2011>498960</tiv_2011>
	<tiv_2012>498960</tiv_2012>
	<eq_site_deductible>792148.9</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>9979.2</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.102261</point_longitude>
	<line>-81.711777</line>
	<construction>Residential</construction>
	<point_granularity>Masonry</point_granularity>
</policy>
<policy>
	<policyId>448094</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>1322376.3</hu_site_limit>
    <fl_site_limit>1322376.3</fl_site_limit>
    <fr_site_limit>1322376.3</fr_site_limit>
    <tiv_2011>1322376.3</tiv_2011>
	<tiv_2012>1322376.3</tiv_2012>
	<eq_site_deductible>1438163.57</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.063936</point_longitude>
	<line>-81.707664</line>
	<construction>Residential</construction>
	<point_granularity>Masonry</point_granularity>
</policy>
<policy>
	<policyId>206893</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>190724.4</hu_site_limit>
    <fl_site_limit>190724.4</fl_site_limit>
    <fr_site_limit>190724.4</fr_site_limit>
    <tiv_2011>190724.4</tiv_2011>
	<tiv_2012>190724.4</tiv_2012>
	<eq_site_deductible>192476.78</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.089579</point_longitude>
	<line>-81.700455</line>
	<construction>Residential</construction>
	<point_granularity>Wood</point_granularity>
</policy>
<policy>
	<policyId>333743</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>0</hu_site_limit>
    <fl_site_limit>79520.76</fl_site_limit>
    <fr_site_limit>0</fr_site_limit>
    <tiv_2011>0</tiv_2011>
	<tiv_2012>79520.76</tiv_2012>
	<eq_site_deductible>86854.48</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.063236</point_longitude>
	<line>-81.707703</line>
	<construction>Residential</construction>
	<point_granularity>Wood</point_granularity>
</policy>
<policy>
	<policyId>172534</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>0</hu_site_limit>
    <fl_site_limit>254281.5</fl_site_limit>
    <fr_site_limit>0</fr_site_limit>
    <tiv_2011>254281.5</tiv_2011>
	<tiv_2012>254281.5</tiv_2012>
	<eq_site_deductible>246144.49</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.060614</point_longitude>
	<line>-81.702675</line>
	<construction>Residential</construction>
	<point_granularity>Wood</point_granularity>
</policy>
<policy>
	<policyId>785275</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>0</hu_site_limit>
    <fl_site_limit>515035.62</fl_site_limit>
    <fr_site_limit>0</fr_site_limit>
    <tiv_2011>0</tiv_2011>
	<tiv_2012>515035.62</tiv_2012>
	<eq_site_deductible>884419.17</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.063236</point_longitude>
	<line>-81.707703</line>
	<construction>Residential</construction>
	<point_granularity>Masonry</point_granularity>
</policy>
<policy>
	<policyId>995932</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>0</hu_site_limit>
    <fl_site_limit>19260000</fl_site_limit>
    <fr_site_limit>0</fr_site_limit>
    <tiv_2011>0</tiv_2011>
	<tiv_2012>19260000</tiv_2012>
	<eq_site_deductible>20610000</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>0</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.102226</point_longitude>
	<line>-81.713882</line>
	<construction>Commercial</construction>
	<point_granularity>Reinforced Concrete</point_granularity>
</policy>
<policy>
	<policyId>223488</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>328500</hu_site_limit>
    <fl_site_limit>328500</fl_site_limit>
    <fr_site_limit>328500</fr_site_limit>
    <tiv_2011>328500</tiv_2011>
	<tiv_2012>328500</tiv_2012>
	<eq_site_deductible>348374.25</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>16425</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.102217</point_longitude>
	<line>-81.707146</line>
	<construction>Residential</construction>
	<point_granularity>Wood</point_granularity>
</policy>
<policy>
	<policyId>433512</policyId>
    <statecode>FL</statecode>
    <eq_site_limit>CLAY COUNTY</eq_site_limit>
    <hu_site_limit>315000</hu_site_limit>
    <fl_site_limit>315000</fl_site_limit>
    <fr_site_limit>315000</fr_site_limit>
    <tiv_2011>315000</tiv_2011>
	<tiv_2012>315000</tiv_2012>
	<eq_site_deductible>265821.57</eq_site_deductible>
	<hu_site_deductible>0</hu_site_deductible>
	<fl_site_deductible>15750</fl_site_deductible>
	<fr_site_deductible>0</fr_site_deductible>
	<point_latitude>0</point_latitude>
	<point_longitude>30.118774</point_longitude>
	<line>-81.704613</line>
	<construction>Residential</construction>
	<point_granularity>Wood</point_granularity>
</policy>

To write into XML file instead of printing into console, you can use the following line of code. This will write into output.xml file with the same output as above.

with open('output.xml', 'w') as f: f.write('\n'.join([convert_row(row) for row in data[1:]]))

That’s all about how to convert CSV data to XML data using Python program.

Source Code

Download

2 thoughts on “How to convert CSV to XML using Python

  1. Hi, Thanks for this tutorial, how to handle blank, zero, null values while converting to XML. The program throws errors and saves the empty files.

    Ex: Let’s say in the above example the “construction” field is blank for some records. How to handle such cases.

    It would be great if you could explain each of the cases – blank, zero, null.
    What type of data imputation/augmentation is to be done to handle such scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *