DCubes
Python module

Lesson 8

CSV and JSON

Read and write tabular CSV and nested JSON with the standard library.

beginner35 minRun on your machine

What you will be able to do

  • Load and write CSV with csv.DictReader and DictWriter
  • Parse and dump JSON with the json module
  • Treat file formats as contracts before pandas

Why this matters for data work

CSV and JSON are the lingua franca of extracts. stdlib first teaches the contract (headers, types as strings, nesting) before pandas hides it. Many jobs still use these modules for small files.

Concepts

CSV — rows of fields. Headers make DictReader natural. Everything arrives as str until you cast.

JSON — objects (dict), arrays (list), nested freely. json.load / json.dump for files; loads / dumps for strings.

UTF-8 — default you should insist on.

Sample file for this lesson: orders-mini.csv (optional download). You can also generate the same rows in the script below.

Practice on your machine

cd ~/dcubes/python-lab
source .venv/bin/activate
mkdir -p data/raw data/out

Write a tiny CSV if you do not download the sample:

cat > data/raw/orders-mini.csv << 'EOF'
order_id,sku,quantity,unit_price
ORD-1,SKU-100,2,9.99
ORD-2,SKU-101,1,15.00
ORD-3,SKU-100,4,9.99
EOF
cat > csv_json_lab.py << 'EOF'
import csv
import json
from pathlib import Path

root = Path(__file__).resolve().parent
raw = root / "data" / "raw" / "orders-mini.csv"
out_json = root / "data" / "out" / "orders-mini.json"

rows = []
with raw.open(newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        rows.append(
            {
                "order_id": row["order_id"],
                "sku": row["sku"],
                "quantity": int(row["quantity"]),
                "unit_price": float(row["unit_price"]),
            }
        )

print("rows", len(rows))
print(rows[0])

with out_json.open("w", encoding="utf-8") as f:
    json.dump(rows, f, indent=2)

loaded = json.loads(out_json.read_text(encoding="utf-8"))
print("json rows", len(loaded))
EOF
python csv_json_lab.py
head -n 20 data/out/orders-mini.json

What you should see

  • rows 3 and a dict for the first order with numeric types.
  • A JSON array file under data/out.

Common mistakes

  • Forgetting newline="" on CSV open (can break newlines on some platforms).
  • Leaving quantities as strings — "2" + "2" is "22".
  • Assuming JSON equals a DataFrame — nested lists need explicit flattening later.

Next

HTTP and simple APIs