Project: To-Do List App with File Storage

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():

python
import json
data = {'name': 'Alice', 'score': 100}
print(json.dumps(data))
Output
{"name": "Alice", "score": 100}

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 JSON
json_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

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 csv
with 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']

Writing delimited rows is just as easy using csv.writer():

python
import csv
with 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

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 csv
with 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

Similarly, csv.DictWriter() makes it simple to write dictionaries back into CSV rows:

python
import csv
with open('dict_out.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['user', 'age'])
    writer.writeheader()
    writer.writerow({'user': 'Dave', 'age': 30})

with open('dict_out.csv', 'r') as f:
    print(f.read().strip())
Output
user,age
Dave,30

Check yourself

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().

Show solution (0 XP)
import json

bad_json = '{"name": "Jane", "age": 25}'
# Fix the errors below
data = json.loads(bad_json)
print(data['name'])

Challenge 2 +25 XP

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 file
with 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'])