Working with CSV & JSON Files
Part of the Python Roadmap
In short
Datasets arrive as CSV files, configs as JSON, LLM outputs as JSON lines. Reading and writing these formats correctly is the plumbing behind every AI project.
The csv module reads and writes rows of spreadsheet-style data, and the json module converts between JSON text and Python dicts and lists. Together they cover most of the data files you will meet.

Think of it like this
The hostel register and the gift box
A CSV is like the hostel entry register: every row is one person, every column the same field. JSON is like a gift box with smaller boxes inside, each labelled. Flat things suit the register, nested things suit the box.
Watch it happen
CSV in, JSON out
1/5with open('students.csv') opens the file and promises to close it when the block ends.
The big ideas
What you need to know
Use with open(...)
The with block closes the file for you. Forgetting to close can lose data that was never flushed to disk.
Pass encoding='utf-8'
Names with Hindi or Tamil characters break without it on some systems. Always say the encoding.
CSV values are all text
'85' is a string until you convert it. The csv module doesn't guess types, pandas does.
JSON maps to dicts and lists
json.load gives you dicts and lists. json.dump writes them back. JSON Lines (.jsonl) stores one JSON object per line.
See it in code
Walk through the code
1import csv2import json3 4with open("students.csv", newline="", encoding="utf-8") as f:5 rows = list(csv.DictReader(f))6 7for row in rows:8 row["marks"] = int(row["marks"])9 row["passed"] = row["marks"] >= 4010 11with open("students.json", "w", encoding="utf-8") as f:12 json.dump(rows, f, indent=2, ensure_ascii=False)13 14with open("students.json", encoding="utf-8") as f:15 back = json.load(f)16print(back[0])1/4Open the CSV and read every row as a dict keyed by the header.
newline="" is what the csv docs recommend when opening CSV files. ensure_ascii=False keeps non-English names readable in the JSON.
Save yourself the pain
Common mistakes
Oops
Opening a file with 'w' when you meant to add to it, wiping everything.
Do this instead
Use 'a' to append. 'w' empties the file the moment it opens.
Oops
Splitting CSV lines with line.split(',') yourself.
Do this instead
Use the csv module. It handles commas inside quoted fields like "Mumbai, MH".
Your mission
Convert a CSV to JSON
Quick check
You read a CSV with csv.DictReader and try row['age'] + 1. What happens?
Go deeper
- 1Read a CSV of students with csv.DictReader and print each name and mark.
- 2Save a list of dicts to a JSON file with json.dump(indent=2).
- 3Remember that every value from a CSV arrives as a string until you convert it.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.