Reading and Writing CSV Files

Introduction

A CSV (comma-separated values) file is one of the simplest and most widely used ways to store tabular data. It is just plain text: each line is a row, and the values in that row are separated by commas. Spreadsheet programs, lab instruments, sensors, and web services all export CSV, which is why engineers run into it constantly. Because it is plain text, you can open a CSV in any editor and see exactly what is there — a useful habit before you write code against it.

Python ships with a built-in csv module, so no third-party installation is required. In this topic you will work with four tools from that module: csv.reader, which gives you each row as a list; csv.DictReader, which uses the header row to give you each row as a dictionary keyed by column name; csv.writer, which writes rows from lists; and csv.DictWriter, which writes rows from dictionaries and can emit the header for you. This connects directly to the file I/O and exception handling work earlier in Module 4 — you still open files with open(), still use with blocks, and still need to handle missing files and malformed data.

As you watch, pay attention to a few specific things: how the file is opened (including the newline='' argument that prevents blank rows on some systems), whether the header row is being consumed or skipped, and when the list-based approach is more convenient than the dictionary-based one. Also notice that everything read from a CSV arrives as a string, so numeric columns must be converted with int() or float() before you can do arithmetic on them.

Video 1: Reading a CSV file in Python

Python Morsels · 3:22 · 1,179 views · 2y ago · captions — Watch on YouTube

About this video

A short, focused screencast on reading a CSV file using only Python's standard library csv module. Based on the title and description, it walks through the helper functions the module provides for reading, showing that no third-party package such as pandas is needed for basic work. Expect to see a file opened and its rows iterated over in a few lines of code.

Why this video

Python Morsels is a well-regarded Python teaching resource, and the tight three-minute format gives you a clean, accurate first look at csv.reader before moving on to the dictionary-based variants.

Video 2: Understanding CSV Files

macmostvideo · 5:21 · 124,781 views · 3y ago · captions — Watch on YouTube

About this video

A conceptual explanation of what CSV files actually are: plain text files that hold spreadsheet-style data, useful for importing and exporting between applications. It is not Python-specific, so it focuses on the format itself — rows, delimiters, and how spreadsheet programs interpret them. This is the background that makes the Python code in the other videos make sense.

Why this video

It directly serves the 'What Is a CSV File?' subtopic, and understanding the format independently of any one language helps you reason about quoting, delimiters, and header rows when your own files misbehave.

Video 3: Python Files - Let's read data from a csv into a dictionary

Python with Andrew · 7:10 · 1,683 views · 3y ago · captions — Watch on YouTube

About this video

A hands-on walkthrough of reading CSV data into dictionaries so that columns can be accessed by name rather than by position. Given the title and description, it demonstrates the pattern behind csv.DictReader and why keying rows by column header makes data easier to work with. Expect practical code you can adapt to your own data files.

Why this video

This covers the csv.DictReader subtopic in a worked, step-by-step way, and it illustrates the key trade-off between list-based and dictionary-based reading that you will need when deciding which writer to use as well.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.