Warming up the neural circuits...
By the end of this chapter you will:
Most data bugs in beginner projects are not algorithm bugs. They are serialization bugs:
This chapter gives you guardrails so your data survives real-world usage.
import json
from pathlib import Path
def load_json(path: Path) -> dict:
if not path.exists():
return {}
try:
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
except json.JSONDecodeError:
return {}
return data if isinstance(data, dict) else {}
def save_json(path: Path, payload: dict) -> None:
tmp = path.with_suffix(".tmp")
with tmp.open("w", encoding="utf-8") as f:
json.dump(payload, f, indent=2, ensure_ascii=False)
tmp.replace(path)Atomic write pattern (tmp.replace) reduces corruption risk on interrupted writes.
import csv
rows = [
{"name": "Alice", "age": "30"},
{"name": "Bob", "age": "25"},
]
with open
payload_v2 = {
"schema_version": 2,
"items": [{"id": 1, "name": "Tea"}],
"currency": "INR",
}Always include schema_version and migration logic once data outlives one script version.
dict, list) before trusting values.| Mistake | Why it hurts | Better move |
|---|---|---|
Blind json.load trust | Runtime key/type failures later | Validate schema immediately |
Writing CSV without newline | Formatting issues on Windows | Use newline="" |
No schema_version | Backward incompatibility | Add versioned format + migrations |
| In-place overwrite without temp file | Partial write corruption | Write temp, then replace |
Path to optional JSON fileValid dict or empty fallback without crashingrows with name and amountCSV file with header and clean row formattingpayload dict and target file pathTarget file replaced atomicallyschema_version = 1 to version 2 format.v1 payload without currency fieldv2 payload with currency and normalized item keysList of potentially malformed recordsClean normalized list with invalid rows skipped/loggedBeginner:
"Why use newline=\"\" when writing CSV on Windows?"
It prevents extra blank lines by letting Python's CSV module control newline formatting.
"What is the difference between json.load and json.loads?"
json.loadreads from a file object.json.loadsparses a JSON string.
Senior:
"How do you protect against partial writes in file-based storage?"
Write to a temporary file and atomically replace the original after successful write.
"Why should serialized payloads include a schema version?"
Schema versioning enables backward-compatible migrations as structure evolves.
newline, headers, encoding).Write safely -> temp file then replace
Read safely -> parse, validate shape, normalize types
CSV safely -> newline="", explicit headers, utf-8
Future-proof -> schema_version + migration helperWhy include schema_version in serialized payloads?
Use newline="" when writing CSV to avoid extra blank lines on Windows.