CSV & XML Files | Dataplexa

Working with CSV & XML Files in Python

CSV and XML are two of the most common file formats used for storing and transporting data. Python provides simple tools to read, write, and process both formats. These formats appear in banking reports, business records, machine outputs, API responses, and almost every data-related workflow.

What Is a CSV File?

CSV stands for Comma-Separated Values. It stores data in a table-like structure where each row represents a record, and each column is separated by a comma. CSV files are extremely common because they are simple to create and easy for machines to read.

Reading CSV Files

Python includes a built-in module called csv that makes it easy to handle CSV files. The reader object reads the file row by row, returning each row as a list. This allows your program to process large datasets efficiently.

import csv

with open("students.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Writing to a CSV File

To add new data to a CSV file, Python uses the writer object. Each row is written as a list, and the module ensures proper formatting. This is commonly used for saving logs, results, reports, and exported data.

import csv

with open("results.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Score"])
    writer.writerow(["Emma", 92])
    writer.writerow(["Daniel", 88])

Using DictReader and DictWriter

These two tools read and write CSV rows as dictionaries instead of lists. This makes the code more readable because each value is linked to a column name. It is helpful when dealing with large datasets.

import csv

with open("students.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row["name"], row["age"])

What Is an XML File?

XML stands for eXtensible Markup Language. It stores data in a structured format using custom tags. XML is widely used in web services, configuration files, documents, and APIs. It is more descriptive than CSV and allows nested structures.

Sample XML Structure

Here is a small example of XML data:

<library>
    <book>
        <title>Ocean Tales</title>
        <author>James Nolan</author>
    </book>
</library>

Reading XML Files

Python provides the xml.etree.ElementTree module for parsing XML files. It allows searching, navigating, and extracting data using tree-like structures. This makes XML processing simple and intuitive.

import xml.etree.ElementTree as ET

tree = ET.parse("library.xml")
root = tree.getroot()

for book in root.findall("book"):
    title = book.find("title").text
    author = book.find("author").text
    print(title, "-", author)

Writing XML Data

You can also generate XML files using Python. This is useful when creating configuration files or exporting structured data. The ElementTree module allows building XML elements and saving them to a file.

import xml.etree.ElementTree as ET

root = ET.Element("library")
book = ET.SubElement(root, "book")

ET.SubElement(book, "title").text = "Ocean Tales"
ET.SubElement(book, "author").text = "James Nolan"

tree = ET.ElementTree(root)
tree.write("output.xml")

CSV vs XML — When to Use What?

Each format serves a different purpose:

  • Use CSV when data is simple, flat, and table-like.
  • Use XML when data needs structure, hierarchy, or descriptive tags.

Choosing the right file type depends on the project’s complexity and how much structure the data requires. Both formats remain popular because they are universal and easy to work with.


📝 Practice Exercises


Exercise 1

Create a CSV file with three columns: name, age, and city. Write three rows of data.

Exercise 2

Read the CSV file and print only the names.

Exercise 3

Create an XML file containing two products with name and price fields.

Exercise 4

Write Python code to read the XML file and print the product names.


✅ Practice Answers


Answer 1

import csv

with open("people.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["name", "age", "city"])
    writer.writerow(["Emma", 28, "London"])
    writer.writerow(["Noah", 25, "Toronto"])
    writer.writerow(["Ava", 32, "Sydney"])

Answer 2

import csv

with open("people.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row["name"])

Answer 3

import xml.etree.ElementTree as ET

root = ET.Element("products")

p1 = ET.SubElement(root, "product")
ET.SubElement(p1, "name").text = "Laptop"
ET.SubElement(p1, "price").text = "1200"

p2 = ET.SubElement(root, "product")
ET.SubElement(p2, "name").text = "Headphones"
ET.SubElement(p2, "price").text = "150"

tree = ET.ElementTree(root)
tree.write("products.xml")

Answer 4

import xml.etree.ElementTree as ET

tree = ET.parse("products.xml")
root = tree.getroot()

for p in root.findall("product"):
    print(p.find("name").text)