Python Convert XML to CSV
In this article, you will learn how to convert XML to CSV file in the Python programming language.
Python has a set of helpful libraries and packages that minimise the use of code in our everyday life. This quality of Python makes it a hot pick among a large portion of the development community, especially for data scientists. Many people favour Python because of its reliability and straightforwardness. A major advantage of this is that it makes it easy to work with huge datasets.
XML is an acronym, standing for Extensible Markup Language. XML plays an important role in web services. With the help of this, we can easily exchange data between different platforms. It provides a structured way to mark up data arranged in a tree like a hierarchy. It is also known as a document representation language. As it reduces complexity and allows data to be read by different incompatible applications, it becomes a good choice in web services and data exchange. In general, XML can be somewhat heavier. You're sending more information, which implies you need more transfer speed, more extra room, and more run time. The data in XML format is not readable by general users. We may need to change it to a more user-friendly format.
CSV file stands for "comma-separated-values", as it uses a comma to separate values. This is a widely used file format that stores data in a tabular format. All the most popular programming languages have tools or applications that support the CSV file format. As the information in XML format is not readable by general clients, we may need to transform it into some easy-to-understand format like CSV. In web development, we can easily import and export to a CSV file and convert any file format to a CSV file. These are the processes to convert XML data to a CSV file.
Suppose we have the following XML file that contains a company's employee records.
<?xml version="1.0"?>
<company>
<employee>
<name>Alaya</name>
<phone>3290349906</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it. </email>
<date>2019-03-02 11:16:07</date>
</employee>
<employee>
<name>Carle</name>
<phone>9059098968</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it. </email>
<date>2019-06-01 10:06:07</date>
</employee>
<employee>
<name>Amma</name>
<phone>6750390948</phone>
<email>This email address is being protected from spambots. You need JavaScript enabled to view it. </email>
<date>2019-04-05 16:30:07</date>
</employee>
</company>
The information stored in this XML file are-
- name
- phone
- date
Here is the complete code to convert an XML to a CSV file using Python. There are several libraries and methods available to parse the XML, but we are using the ElementTree module. It parses the whole XML document and stores it in the form of a tree. It provides the parse() method to parse the XML. The tree has a getroot() method that returns the root element of the tree. Using a for loop iteration, we iterate over the tree and access the data using the tag names (name, phone, email, and date). The parsed data is stored in a DataFrame using the Python Pandas module. It is a two-dimensional data structure.
import xml.etree.ElementTree as ET
import pandas as pd
cols = ["name", "phone", "email", "date"]
rows = []
#Parse XML file
tree = ET.parse('employee.xml')
root = tree.getroot()
for elem in root:
name = elem.find("name").text
phone = elem.find("phone").text
email = elem.find("email").text
date = elem.find("date").text
rows.append({"name": name,
"phone": phone,
"email": email,
"date": date})
df = pd.DataFrame(rows, columns = cols)
# write dataframe to csv
df.to_csv('company.csv')
The above code generates the CSV file something like this-
Related Articles
Read xml file PythonPython Converting a CSV File to a MySQL Table
Python Pandas CSV to Dataframe
How to convert Excel to CSV Python Pandas
Python convert XML to JSON
Insert XML data to MongoDB using Python
Python parse XML with lxml library
Insert XML Data to MySQL Table using Python
Python NumPy tutorial
Python Pillow resize image
OpenCV histogram equalization color
Color histogram Python OpenCV
Histogram of grayscale image python
High pass filter OpenCV python
Python OpenCV ColorMap
OpenCV Gaussian blur Python
Python OpenCV Overview and Examples
Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup