Python CSV & JSON File Processing

๐Ÿ Python 3.12+ ๐ŸŸข Chapter 27 of 65 ๐Ÿ“‚ Phase 6: Exception and File Handling ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: csv.reader ยท csv.writer ยท csv.DictReader ยท csv.DictWriter ยท json.dump() & load() ยท Structured Storage
Master structured tabular and document data processing in Python: reading and writing CSV files with csv.reader and csv.DictReader, parsing nested JSON documents with json.dump() and json.load(), and building real-world data pipelines.
1Tabular Data with CSV: csv.reader and csv.writer

CSV (Comma-Separated Values) is the standard format for tabular spreadsheet data across Excel, Google Sheets, databases, and data science pipelines.

Python ships with the built-in csv module, which automatically handles escaped quotation marks, embedded commas in text, and platform newline quirks:

๐Ÿ’ก The newline="" Rule in CSV Writing:
Always specify newline="" when opening files for writing with the CSV module (open("data.csv", "w", newline="", encoding="utf-8")). This prevents Python from inserting blank empty rows on Windows operating systems!
๐Ÿ’ป Example 1: Reading and Writing CSV Files with csv.reader and writer
import csv

# 1. Writing tabular records to a CSV file:
student_data = [
    ["Roll No", "Student Name", "Grade", "Percentage"],
    [101, "Balaji", "A+", 94.5],
    [102, "Alex Smith", "A", 88.0],
    [103, "Chloe Davis", "A+", 96.2],
    [104, "David Brown", "B", 72.4]
]

with open("students.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerows(student_data)
print("โœ… students.csv written successfully!")

# 2. Reading tabular records back with csv.reader:
print("\n--- Reading students.csv ---")
with open("students.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    header = next(reader) # Extract first row as column header
    print("Columns Header:", header)
    
    for row in reader:
        print(f"Roll: {row[0]:4} | Name: {row[1]:12} | Grade: {row[2]} ({row[3]}%)")
๐Ÿ” Method Breakdown:
  • writer.writerows(list_of_lists) writes all 2D table rows in one efficient batch call.
  • next(reader) advances the iterator by 1 row, cleanly stripping out the header before looping over data rows.
2Professional Column Mapping: csv.DictReader & csv.DictWriter

Accessing CSV columns by numeric indexes (like row[1]) is brittle: if a developer adds a new column, all indexes shift and break your code. csv.DictReader and csv.DictWriter map rows directly to Python dictionaries using header keys:

๐Ÿ’ป Example 2: Robust Column Mapping with csv.DictReader and csv.DictWriter
import csv

# 1. Writing CSV using Dictionary rows (DictWriter):
fieldnames = ["product_id", "product_name", "category", "price"]

products = [
    {"product_id": "P01", "product_name": "Mechanical Keyboard", "category": "Hardware", "price": 2499.00},
    {"product_id": "P02", "product_name": "Wireless Mouse", "category": "Hardware", "price": 799.00},
    {"product_id": "P03", "product_name": "USB-C Hub", "category": "Accessories", "price": 1299.00}
]

with open("inventory.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader() # Automatically writes the first header row!
    writer.writerows(products)

# 2. Reading CSV as clean dictionaries (DictReader):
print("--- Inventory Catalog (DictReader) ---")
with open("inventory.csv", "r", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(f"[{row['product_id']}] {row['product_name']:20} | Rs.{float(row['price']):.2f}")
๐Ÿ” Why DictReader is Superior:

You access fields by explicit name: row['product_name']. Even if columns in the CSV are reordered, your code continues functioning perfectly without changes!

3Hierarchical Structured Storage with JSON (dump & load)

While CSV is great for flat tabular data, modern web applications, configurations, and nested data schemas use JSON (JavaScript Object Notation).

  • json.dump(obj, file_stream, indent=4): Serializes Python dictionaries/lists directly to a disk file stream.
  • json.load(file_stream): Reads and parses a JSON file directly into native Python dictionaries and lists.
๐Ÿ’ป Example 3: Serializing and Deserializing JSON Data to Disk
import json

# Nested hierarchical application configuration:
app_settings = {
    "app_name": "ManaCompiler Pro",
    "version": "3.5.0",
    "server": {
        "host": "0.0.0.0",
        "port": 8080,
        "ssl_enabled": True
    },
    "features": ["code_editor", "realtime_compiler", "ai_code_review"],
    "supported_languages": ["Python", "Java", "C", "JavaScript"]
}

# 1. Save complex dictionary to physical JSON file on disk:
with open("app_config.json", "w", encoding="utf-8") as f:
    json.dump(app_settings, f, indent=4)
print("โœ… app_config.json saved to disk!")

# 2. Read JSON file back from disk into Python dictionary:
with open("app_config.json", "r", encoding="utf-8") as f:
    loaded_config = json.load(f)

print("\n--- Loaded JSON Config ---")
print("App Name: ", loaded_config["app_name"])
print("Host URL: ", f"http://{loaded_config['server']['host']}:{loaded_config['server']['port']}")
print("Languages:", ", ".join(loaded_config["supported_languages"]))
๐Ÿ” CSV vs JSON:
  • Use CSV for 2D flat tables (spreadsheets, bank statements, sensor time series).
  • Use JSON for nested hierarchical trees (user profiles with sub-arrays, API requests, configuration files).
โš ๏ธ Common Developer Pitfall: Forgetting newline="" When Writing CSV on Windows

Omitting newline="" when calling open("data.csv", "w") causes the Windows standard C runtime to write \r\r\n line endings, resulting in unwanted blank empty rows between every record in Excel. Always pass newline="".

๐Ÿ’ป Hands-on Interactive Practice Challenge

Create a list of dictionaries representing 3 books (title, author, price). Save them to a CSV file named "books.csv" using DictWriter and print the file contents.

Python 3 Practice Challenge โ–ถ Run in Compiler
import csv

books = [
    {"title": "Clean Code", "author": "Robert C. Martin", "price": 450},
    {"title": "Fluent Python", "author": "Luciano Ramalho", "price": 890},
    {"title": "The Pragmatic Programmer", "author": "Andrew Hunt", "price": 550}
]

with open("books.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["title", "author", "price"])
    writer.writeheader()
    writer.writerows(books)

print("Saved books.csv successfully! Reading back:")
with open("books.csv", "r", encoding="utf-8") as f:
    print(f.read().strip())
Run This Challenge in Online Python IDE โ†’
โ“ Frequently Asked Questions (FAQ)

Q What is the difference between json.dumps() and json.dump()?

json.dumps(obj) converts a Python object into a string in RAM. json.dump(obj, file) writes the serialized JSON directly into an open file stream on disk.

Q How do I handle CSV files that use semicolons (;) or tabs (\t) instead of commas?

Pass delimiter=";" or delimiter="\t" into csv.reader() or csv.writer(): csv.reader(f, delimiter=";").

Q Can CSV files store nested objects or lists?

No. The standard CSV format is strictly 2D flat tabular text. To store nested arrays or dictionaries, use JSON, YAML, or SQLite.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Python 3.12+ runtime ยท Last updated August 2026