Convert XML to CSV in Python Last Updated : 11 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.ApproachImport 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.ExamplePython Program to Convert XML to CSV. Python # Importing the required libraries import xml.etree.ElementTree as Xet import pandas as pd cols = ["name", "phone", "email", "date", "country"] rows = [] # Parsing the XML file xmlparse = Xet.parse(r'psth_to_sample.xml') root = xmlparse.getroot() for i in root: name = i.find("name").text phone = i.find("phone").text email = i.find("email").text date = i.find("date").text country = i.find("country").text rows.append({"name": name, "phone": phone, "email": email, "date": date, "country": country}) df = pd.DataFrame(rows, columns=cols) # Writing dataframe to csv df.to_csv('output.csv') Output:Snapshot of the saved fileCode 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 More infoAdvertise with us Next Article Convert PDF to CSV using Python P pawki Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 python-csv Python-XML +1 More Practice Tags : python Similar Reads Convert JSON to CSV in Python We can convert JSON to CSV in Python using the built-in json and csv modules.What is JSON and CSV?JSON is a lightweight, text-based data format commonly used to exchange data between web servers and clients. It stores data in key-value pairs and is typically used for data transmission in web applica 2 min read Convert Excel to CSV in Python In this article, we will discuss how to convert an Excel (.xlsx) file to a CSV (.csv) file using Python. Excel files are commonly used to store data, and sometimes you may need to convert these files into CSV format for better compatibility or easier processing.Excel File Formats.xlsx: The newer Exc 3 min read Convert TSV to TXT in Python In this article, we are going to see how to convert TSV files to text files in Python. Approach:Open TSV file using open() functionOpen txt file in which we are going to write TSV file dataThen use csv.reader() it will return a reader object which will iterate over lines in the given TSV file. (set 2 min read Convert PDF to CSV using Python Python is a high-level, general-purpose, and very popular programming language. Python programming language (the latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. Python Programming Language is very well sui 2 min read Convert CSV to HTML Table in Python CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.Example :  Name, Salary, Age, No.of year 2 min read Convert nested JSON to CSV in Python In this article, we will discuss how can we convert nested JSON to CSV in Python. An example of a simple JSON file: A simple JSON representationAs you can see in the example, a single key-value pair is separated by a colon (:) whereas each key-value pairs are separated by a comma (,). Here, "name", 9 min read Like