Convert XML to CSV in Python Last Updated : 11 Apr, 2025 Comments Improve Suggest changes 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 XML to CSV in 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 be dealing with the conversion of Excel (.xlsx) file into .csv.  There are two formats mostly used in Excel : (*.xlsx) : Excel Microsoft Office Open XML Format Spreadsheet file.(*.xls) : Excel Spreadsheet (Excel 97-2003 workbook). Let's Consider a dataset of a shopping store 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 Convert CSV to Excel using Pandas in Python Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. Syntax 1 min read Convert multiple JSON files to CSV Python In this article, we will learn how to convert multiple JSON files to CSV file in Python. Before that just recall some terms : JSON File:  A JSON file may be a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which may be a standard data interchange for 8 min read How to Convert JSON to Excel in Python JSON (JavaScript Object Notation) is a widely used data format that is both easy to read and write for humans and simple for machines to parse and generate. However, there may be times when we need to convert this JSON data into an Excel spreadsheet for analysis or reporting. In this article, we'll 3 min read Convert Text File to CSV using Python Pandas Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library.In this article we will walk you through multip 2 min read Like