CSV and JSON
Scenario: A service exports rows for spreadsheets and configuration for APIs.
New words, in plain English
- CSV - tabular text where rows contain separated fields
- JSON - a text format for objects, arrays, strings, numbers, booleans, and null
- Serialization - turning in-memory data into a storable format
Use csv.DictReader/DictWriter instead of splitting commas yourself. Use json.load/dump for files and loads/dumps for strings. JSON objects become dicts; arrays become lists.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
import csv, json
with open("services.csv", newline="") as handle:
rows = list(csv.DictReader(handle))
with open("services.json", "w") as handle:
json.dump(rows, handle, indent=2)
Tip: Open CSV with newline="" so the csv module controls line endings.
Goal: Practice this idea in the py-csv-pathlib challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.