In this project you will write a Python function that reads a CSV file and turns it into data your program can work with — without using any special libraries. Just Python basics: opening files, reading lines, and working with strings.
This project requires Python 3 and pytest. Python 3 should already be installed from your path setup — if anything seems off, refer back to the installation videos before continuing.
Confirm Python is installed:
python3 --versionpytest may or may not have been covered in your path. Check if it is installed:
pytest --versionIf that returns a version number, you are good to go. If you get a command not found error, install it with:
pip install pytestA CSV (Comma-Separated Values) file is a simple text file used to store table data. Each line is a row, and commas separate the columns.
name,age,city
Ada,32,Nashville
Grace,28,Atlanta
The first line is the header — it names the columns. Every line after that is a data row.
You will complete the read_csv function inside reader.py. It takes a file path as input and returns a list of dictionaries — one dictionary per data row.
Each dictionary uses the column headers as keys:
[
{'name': 'Ada', 'age': '32', 'city': 'Nashville'},
{'name': 'Grace', 'age': '28', 'city': 'Atlanta'},
]Think of it like turning each row of a spreadsheet into a labeled set of values.
- Do not use the
csvmodule,pandas, or any other library for parsing. - Use only what Python gives you by default: opening files, reading lines, and string methods like
.split(),.strip(), and.zip().
This constraint is intentional — the point is to practice thinking through the problem yourself.
Open reader.py. You will see this:
def read_csv(filepath):
"""Read a CSV file and return a list of dicts."""
# your code here
passReplace pass with your implementation. Here is a suggested order to work through it:
- Open the file and read its lines
- Use the first line as the header to get your column names
- For each remaining line, pair each value with its column name to make a dictionary
- Handle edge cases: empty files, files with only a header, and blank lines in the middle
- Strip extra whitespace from keys and values
There are four CSV files in the data/ folder. Each one tests a different situation:
| File | What it contains |
|---|---|
data/simple.csv |
A clean, normal CSV — 1 header row + 2 data rows |
data/messy.csv |
Values have extra spaces; there is a blank line between rows |
data/header_only.csv |
Only a header row, no data rows |
data/empty.csv |
Completely empty |
Your function needs to handle all four correctly.
Your function is complete when all 10 tests pass. Here is what each test is checking:
| Test | What it checks |
|---|---|
test_simple_file_row_count |
A normal CSV with 2 data rows returns a list of length 2 |
test_simple_file_keys |
Dictionary keys match the header (name, age, city) in order |
test_simple_file_first_row_values |
All three values in the first row are correct |
test_simple_file_second_row_values |
All three values in the second row are correct |
test_empty_file |
A completely empty file returns [] |
test_header_only_file |
A file with only a header and no data returns [] |
test_messy_file_skips_blank_rows |
Blank lines in the file are ignored |
test_whitespace_stripped_from_values |
Extra spaces around values are removed |
test_whitespace_stripped_from_keys |
Extra spaces around column headers (keys) are removed |
test_messy_file_second_row_values |
The second row in a messy file is still parsed correctly |
Run the tests from inside the csv_reader_project/ folder:
python3 -m pytest test_reader.py -vThe -v flag shows each test by name so you can see exactly which ones are passing and which are not.
When you start, all tests will fail. That is expected! Work through them one at a time.
You can also run the script directly to see your output:
python3 reader.pyThis reads data/simple.csv and prints the results so you can see what your function is returning.
- Read the test file.
test_reader.pytells you exactly what your function should return for each input. It is your spec. - Start simple. Get the basic case working first (
simple.csv), then handle the edge cases one at a time. - Print things out. If something is not working, add
print()statements to see what your code is actually producing. - Look up string methods.
.split(','),.strip(), andzip()will be your best friends here.