VOOZH about

URL: https://www.geeksforgeeks.org/python/convert-xml-to-csv-in-python/

⇱ Convert XML to CSV in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert XML to CSV in Python

Last Updated : 23 Jul, 2025

XML (Extensible Markup Language) is widely used to store and transport structured data. However, for tasks like data analysis or spreadsheet editing, CSV (Comma-Separated Values) is far more user-friendly and easier to work with.

To make XML data easier to process, we often convert it to a CSV file. In this article, we will explore how to convert XML to CSV step-by-step with the help of the built-in xml.etree.ElementTree module and the powerful pandas library.

Approach

  • Import necessary libraries.
  • Define the desired column headers.
  • Parse the XML file.
  • Extract the relevant data.
  • Store it into a pandas DataFrame.
  • Export the DataFrame to a CSV file.

To download the XML data used in the examples, click here.

Example

Python Program to Convert XML to CSV.

Output:

👁 XML-CSV
Snapshot of the saved file

Code Breakdown:

  • We used ElementTree to parse and navigate through the XML structure.
  • Data from each record was collected into a list of dictionaries.
  • Finally, we used pandas to create a CSV file from that structured data.

To learn about the pandas module in depth, refer to: Python Pandas Tutorial

Comment