Skip to content
Tactiverge

Learning Rooms/○ Level 3 · Innovator/Computing and algorithms

Python Tutorial: CSV Module - How to Read, Parse, and Write CSV Files

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Load a CSV file into a list of dictionaries with csv.DictReader.
  • Convert text fields to numbers safely and handle missing or bad values.
  • Group and summarise records with dictionaries.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

csv.DictReader reads the row 'run,current_a' / '3,1.72'. What is type(row['current_a'])?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute (borrowed from the lesson “Slice, filter, transform: lists at speed”, on the same idea). It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The list visualiser shows readings = [12, 15, 11, 18, 22, 19, 25, 21] with each index from 0 to 7 (and −8 to −1). Typing start, stop and step highlights the chosen items: 1, 6, 2 selects 15, 18 and 19; a negative step walks backwards; a step of 0 gives a ValueError.

3.Watch, actively

Reads and writes real CSV files with csv.reader and csv.DictReader, the same tools you use on a sensor log.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how DictReader uses the header row as keys, and how the writer produces a CSV from dictionaries.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

A CSV file stores a table as plain text: one row per line, values separated by commas. Python's csv module splits lines correctly even when values contain commas, and DictReader gives each row as a dictionary keyed by the header, so code can say row['temp'] instead of row[3].

What the video covers (our own note, written when we verified it): Reads and writes real CSV files with csv.reader and csv.DictReader, the same tools you use on a sensor log.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

Some rows have current 'n/a'. Which policy is best for an engineering report?

Practice 2

Which line adds current to a per-motor list, creating the list the first time a motor appears?

Practice 3 · a fresh version every time

A log of motor temperature (°C) reads xs = [42, 50, 49, 47, 47, 44, 43, 44, 47]. What is the mean, statistics.mean(xs)? Give 2 decimal places.

Practice 4

row = {'run': '4', 'current_a': '1.5'} from DictReader. What does row['run'] + 1 do?

6.Where engineers use it

Test engineers often keep raw rig logs untouched and write loaders that report how many rows were rejected and why, so the cleaning step is visible in the final report.

From the applied view in the lesson From CSV file to records you can query. Checked by automated tests only.

7.Build something with it

Follow-up project

Write a loader for a part inventory CSV (part_no, description, qty, mass_g) that returns a list of dicts and a count of rejected rows. Use it to answer: which parts have qty below 10, and what is the total mass in stock in kg? Test the loader on a string containing at least one bad row.

Where this video lives

In the course catalogue: CSE-310 Structured programs and data

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.