| by Arround The Web | No comments

Python XML to CSV

XML” stands for extensible markup language. It is important for web services because it helps transfer data between different platforms easily. It arranges/organizes data in a hierarchy or tree structure. “CSV” or comma-separated values is a way to organize data in a table using commas, and it’s supported by lots of programming languages. Sometimes we need to convert XML to CSV in Python for data manipulation. To do this, different/various methods are utilized in Python.

This Python write-up will present a complete guide on how to convert XML to CSV using numerous examples.

How to Convert Python XML to CSV?

To convert Python XML to CSV, the following methods are used in Python:

The following XML file is used throughout this article:

Method 1: Convert Python XML to CSV Using the “xmltodict” Module

The “Xmltodict” is a Python module that changes XML information into a dictionary. It is a speedy and effective tool that can manage big XML files. This module is used along with the “CSV” module to convert Python XML to CSV.

Before going to the example, you need to install the “xmltodict” module via the below code:

pip install xmltodict

Let’s utilize the below code to convert XML to CSV:

import xmltodict
import csv
with open("students.xml") as xmlfile:
    xml = xmltodict.parse(xmlfile.read())
csv1 = open("students.csv", 'w', encoding='utf-8')
csv2 = csv.writer(csv1)
csv2.writerow(["name", "major", "age"])
for student in xml["students"]["student"]:
    csv2.writerow([student["name"], student["major"], student["age"]])
csv1.close()

In the above code:

  • The “xmltodict” and “csv” modules are imported first.
  • The “with open()” statement opens the “students.xml” file in read mode.
  • After that, the opened file is passed to the “xmltodict.parse()” function to convert it into a Python dictionary.
  • Next, the “open()” statement opens the “students.csv” CSV file in write mode.
  • The “csv2.writerow()” method is used to write a header row to a CSV file. Here the header row contains the names of the columns in the comma-separated value file.
  • Next, the “for” loop iterates over the “students” dictionary and for each student in the dictionary, it extracts the XML data such as name, major, and age.
  • Finally, these values are passed to the “csv2.writerow()” method to write them to the CSV file.

Output

The XML has been transferred into CSV.

Method 2: Convert Python XML to CSV Using the “ElementTree” Module

We can also use a different way to change XML to CSV without needing to add more libraries. Python has a module named “xml.etree.ElementTree” that can handle XML. By utilizing this module, we can easily read and write XML files via the following code:

from xml.etree import ElementTree
import csv
xml = ElementTree.parse("students.xml")
csv1 = open("students.csv",'w',encoding='utf-8')
csv2 = csv.writer(csv1)
csv2.writerow(["name","major","age"])
for student in xml.findall("student"):  
    if(student):
      name = student.find("name").text
      major = student.find("major").text
      age = student.find("age").text
      csv2.writerow([name, major, age])
csv1.close()

Here in this code:

  • We first imported the “ElementTree” and “CSV” modules.
  • Next, the “ElementTree.parse()” parses the “XML” file, and the “open()” function opens the.csv file in write mode.
  • After that, the writer object is created using the “csv.writer()” method and the header row is written to the CSV file using the “writerow()” method.
  • The “for” loop iterates over all the student elements in the XML data and checks if the current student element is empty or not.
  • The “find()” is used with the “.text” attribute to extract the name, major and age text from the current student element.
  • Lastly, the “writerow()” writes the name, major, and age values to the CSV file.

Output

The XML has been transferred into CSV.

Method 3: Convert Python XML to CSV Using the untangle Module

We can change XML to CSV by using a Python module called “untangle”. Like the “xmltodict” module, the “untangle” examines an XML file and makes the information simple to retrieve. Install this module via the following code before going to the example:

pip install untangle

Here is an example code that converts XML to CSV:

import untangle
import csv
xml = untangle.parse("students.xml")
csv1 = open("students.csv",'w',encoding='utf-8')
csv2 = csv.writer(csv1)
csv2.writerow(["name","major","age"])
for student in xml.students.student:
    csv2.writerow([student.name.cdata, student.major.cdata, student.age.cdata])
csv1.close()

In this code:

  • We imported the “untangle” library and the “csv” library.
  • Next, the XML file “students.xml” file is parsed using the “untangle.parse()” method into a Python object.
  • After that, a CSV file is created and opened in write mode.
  • Next, we construct a “csv.writer” object and write the row of headers to the comma-separated value file.
  • The for loop iterates over the “students” element in the XML file, and for each student, we write the student’s name, major, and age to the CSV file.

Output

Conclusion

The “xmltodict” module, “ElementTree” module and the “untangle” module are utilized to convert Python XML to CSV (Comma Separated Value). The Python built-in “xml.etree.ElementTree” module is an easy way to convert the XML file into CSV without installing an extra module. This write-up delivered a detailed guide on Python XML into CSV using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply