Python 3.14 ·
✓ verified by execution on 2026-07-25
When building a real application like a To-Do List, you need to save the user’s data so it persists after the program closes. The most common ways to serialize data in Python are using the JSON and CSV formats.
Working with JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. Python parses JSON into standard native types like dicts, lists, strings, and booleans.
To convert a Python dictionary into a JSON string, you use json.dumps():
If you are reading from a file, use json.load() to directly deserialize the file-like object into a Python object:
python
import json# Simulating a file containing JSONjson_str = '{"status": "ok", "count": 5}'with open('data.json', 'w') as f: f.write(json_str)with open('data.json', 'r') as f: parsed = json.load(f)print(parsed['count'])
Output
5
Your output
Working with CSV files
CSV (Comma Separated Values) is a classic format for tabular data. Instead of manually splitting strings with .split(',')—which breaks if commas are inside quoted fields—you should always use the csv module.
You can read a CSV file row by row as lists:
python
import csvwith open('temp.csv', 'w', newline='') as f: f.write('id,name\n1,Alice\n2,Bob')with open('temp.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row)
Output
['id', 'name']
['1', 'Alice']
['2', 'Bob']
Your output
Writing delimited rows is just as easy using csv.writer():
python
import csvwith open('out.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['item', 'cost']) writer.writerow(['apple', 1.5])with open('out.csv', 'r') as f: print(f.read().strip())
Output
item,cost
apple,1.5
Your output
DictReader and DictWriter
If your CSV has a header row, it is usually much safer and easier to read each row as a dictionary using csv.DictReader():
python
import csvwith open('dict.csv', 'w', newline='') as f: f.write('id,name\n10,Charlie')with open('dict.csv', 'r') as f: reader = csv.DictReader(f) for row in reader: print(row['name'])
Output
Charlie
Your output
Similarly, csv.DictWriter() makes it simple to write dictionaries back into CSV rows:
Why is the csv module preferred over manually calling .split(',') on a text file line?
Reveal answer
Manual splitting breaks if commas are present inside quoted fields, whereas the csv module handles these edge cases correctly. — Manual splitting breaks if commas are inside quoted fields; always use the csv module to handle edge cases.
Which method should you use to convert a Python dictionary into a JSON string?
Reveal answer
json.dumps() — json.dumps() stands for 'dump string'. It serializes a Python object into a JSON formatted string.
What is the difference between json.load() and json.loads()?
Reveal answer
json.load() reads from a file-like object, while json.loads() parses a string. — json.loads() is used for strings ('s' for string), whereas json.load() is used for reading directly from a file.
Challenges
Challenge 1 +25 XP
Fix the bug! The code attempts to read a JSON string but it fails. Correct the function used and fix the syntax error in the JSON string so it successfully prints 'Jane'.
python
Test 1 — expects "Jane\n"
Need a hint? (−25% XP)
Remove the trailing comma in the JSON string, and use json.loads() for strings instead of json.load().
Complete the script to use csv.DictReader to print the 'score' for each row.
python
Test 1 — expects "90\n85\n"
Need a hint? (−25% XP)
Initialize csv.DictReader(f) and iterate over it, printing row['score'].
Show solution (0 XP)
import csv# Setup test filewith open('scores.csv', 'w') as f: f.write('name,score\nAlice,90\nBob,85')with open('scores.csv', 'r') as f: # Write your DictReader code here reader = csv.DictReader(f) for row in reader: print(row['score'])