Skip to content

Repped-Labs/csv-reader-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

CSV Reader Project

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.


Prerequisites

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 --version

pytest may or may not have been covered in your path. Check if it is installed:

pytest --version

If that returns a version number, you are good to go. If you get a command not found error, install it with:

pip install pytest

What is a CSV file?

A 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.


What You Will Build

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.


The Rules

  • Do not use the csv module, 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.


Where to Start

Open reader.py. You will see this:

def read_csv(filepath):
    """Read a CSV file and return a list of dicts."""
    # your code here
    pass

Replace pass with your implementation. Here is a suggested order to work through it:

  1. Open the file and read its lines
  2. Use the first line as the header to get your column names
  3. For each remaining line, pair each value with its column name to make a dictionary
  4. Handle edge cases: empty files, files with only a header, and blank lines in the middle
  5. Strip extra whitespace from keys and values

The Data Files

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.


Acceptance Criteria

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

Running the Tests

Run the tests from inside the csv_reader_project/ folder:

python3 -m pytest test_reader.py -v

The -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.


Trying It Manually

You can also run the script directly to see your output:

python3 reader.py

This reads data/simple.csv and prints the results so you can see what your function is returning.


Tips

  • Read the test file. test_reader.py tells 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(), and zip() will be your best friends here.

About

A beginner-friendly Python CSV reader project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages