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.
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 :
<State>
<Resident Id="100">
<Name>John Doe</Name>
<Phone>1234567891</Phone>
<Email>Johndoe@example.com</Email>
</Resident>
<Resident Id="101">
<Name>Jane Doe</Name>
<Phone>1234567891</Phone>
<Email>Janedoe@example.com</Email>
</Resident>
.
.
.
.
</State>
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:
- Id: 100
- Name: John Doe
- Phone: 1234567891
- Email: Johndoe@example.com
- Id: 101
- Name: Jane Doe
- Phone: 1234567891
- Email: Janedoe@example.com
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 resident_head.append(name) Phone = member.find('Phone').tag resident_head.append(Phone) Email = member.find('Email').tag resident_head.append(Email) csvwriter.writerow(resident_head) count = count + 1 name = member.find('Name').text resident.append(name) Phone = member.find('Phone').text resident.append(Phone) Email = member.find('Email').text resident.append(Email) csvwriter.writerow(resident) Resident_data.close()
The output of above code:
Name | PhoneNumber | EmailAddress |
---|---|---|
John Doe | 1234567891 | Johndoe@example.com |
Jane Doe | 1234567891 | Janedoe@example.com |
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
- Using Pandas to CSV() with Perfection
- Python int to Binary | Integer to Binary Conversion
- [Best] Ways to Delete a File in Python
- Convert Text File to PDF Using Python | FPDF
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!
The post XML to CSV Conversion Using Python appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment