Python Pool: XML to CSV Conversion Using Python

Hello coders!! In this article we will learn how to convert an xml file into a csv file in python. So without much ado lets directly dive into the topic.

Contents of Tutorial

What is XML?

XML stands for Extensible Markup Language. It is a hierarchical data used to track data (small to medium amount) without using SQL as the backbone. It is designed to store the data and also transfer it. As a result, the XML format data is not very user-friendly, which gives rise to the need to change the format into a more easy and user-friendly one, like CSV.

What is CSV?

CSV stands for Comma Separated Values. As the name suggests, the data is separated using comma as delimiters. It is the most used format for import and export format of spreadsheets and databases. 

Converting an xml to csv file:

Consider the following XML file :

<Resident Id="100">
<Name>John Doe</Name>
	<Email>[email protected]</Email>
<Resident Id="101">
	<Name>Jane Doe</Name>
	<Email>[email protected]</Email>

In this xml file we are storing the details of the residents of a state. The information stored are:

  • Id
  • Name
  • Phone Number
  • Email Address

Here, we have taken only two details as an example:

Python Program to Convert XML to CSV

From the above example, we can understand that if the number of residents increases, it becomes difficult to read and understand the data. This is the reason why we will now convert this XML format file into a CSV format file.

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("sample.xml")
root = tree.getroot()

Resident_data = open('ResidentData.csv', 'w')

csvwriter = csv.writer(Resident_data)
resident_head = []

count = 0
for member in root.findall('Resident'):
	resident = []
	address_list = []
	if count == 0:
		name = member.find('Name').tag
		Phone = member.find('Phone').tag
		Email = member.find('Email').tag
		count = count + 1

	name = member.find('Name').text
	Phone = member.find('Phone').text
	Email = member.find('Email').text


The output of above code:


Explanation of code for Converting Python XML to CSV:

At first,two modules are imported:

  • xml.etree.ElementTree: to create a simple API for parsing and implementing XML data
  • CSV: to access the data in CSV format

The next two functions is used to parse through the xml file:

  • parse(): to parses= the ‘Sample.xml’ file
  • getroot(): returns the root element of ‘Sample.xml’

Next, we have opened a file ResidentData.csv dor writing

We then created a CSV writer object Resident_data

Then We iterated through the XML file, starting from the root element

We directly added the details of the root element to our CSV format file and incremented the counter value

For other entries we appended the details to the CSV format file without changing the counter value

After each entry we have also closed the particular row

When all the data is entered, we close the CSV format file as well.

Must Read

Conclusion: Python XML to CSV

In this article, we learned about the conversion of Python XML and CSV format. We saw examples for each and further learned a python implementation of converting an XML file to a CSV file in Python.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Author: Shantun Parmar

3 thoughts on “Python Pool: XML to CSV Conversion Using Python

  1. I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let most people without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  2. I needed to draft you that little bit of observation so as to say thanks once again for your lovely opinions you’ve documented in this article. It is quite remarkably generous of people like you to deliver openly exactly what a number of us might have marketed as an e book to end up making some dough for themselves, most importantly given that you might well have tried it in the event you wanted. Those tactics in addition worked to become great way to realize that someone else have the same keenness similar to my very own to realize significantly more with reference to this problem. I know there are some more pleasurable situations up front for people who scan through your website.

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.