0Pricing
Python Academy · Lesson

Reading CSV with csv.reader

Read CSV files row by row and access columns by index.

Reading CSV with csv.reader is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Python's csv module handles the complexities of CSV files: quoted fields, delimiters, and different line endings.

Opening a CSV File

with open('data.csv', newline='') as f: is the recommended way — newline='' lets the csv module handle line endings.
import csv, io
sample = 'name,age,city\nAlice,30,NYC\nBob,25,LA'
reader = csv.reader(io.StringIO(sample))
for row in reader:
    print(row)

csv.reader Basics

csv.reader(f) returns an iterator of rows, where each row is a list of strings.
import csv, io
data = 'a,b,c\n1,2,3\n4,5,6'
for row in csv.reader(io.StringIO(data)):
    print(row)

Skipping the Header

next(reader) skips the header row. Or use csv.DictReader to access columns by name.
import csv, io
data = 'name,age\nAlice,30\nBob,25'
reader = csv.reader(io.StringIO(data))
header = next(reader)  # skip
for row in reader:
    print(row)

Custom Delimiter

csv.reader(f, delimiter=';') reads semicolon-separated files. Works with any single-character delimiter.
import csv, io
data = 'a;b;c\n1;2;3'
for row in csv.reader(io.StringIO(data), delimiter=';'):
    print(row)

Handling Quoted Fields

csv.reader handles quoted fields automatically: 'Alice, Jr.' stays as one field.
import csv, io
data = 'name,note\n"Smith, John","great guy"'
for row in csv.reader(io.StringIO(data)):
    print(row)

csv.DictReader

DictReader maps each row to an OrderedDict (or plain dict in 3.8+) using the header row as keys.
import csv, io
data = 'name,age\nAlice,30\nBob,25'
for row in csv.DictReader(io.StringIO(data)):
    print(row['name'], row['age'])

Type Conversion

CSV values are always strings. Convert explicitly: int(row['age']), float(row['price']).
import csv, io
data = 'item,price\napple,1.5\nbanana,0.75'
for row in csv.DictReader(io.StringIO(data)):
    print(row['item'], float(row['price'])*2)

Handling Encoding

open(file, encoding='utf-8-sig') handles UTF-8 BOM (common in Excel-generated CSVs).
# with open('data.csv', encoding='utf-8-sig', newline='') as f:
#     reader = csv.DictReader(f)
print('encoding demo')

Large CSV Files

csv.reader is lazy — it reads one row at a time. No need to load the whole file into memory.
import csv, io
data = '\n'.join(f'{i},{i*2}' for i in range(1000))
reader = csv.reader(io.StringIO(data))
print(sum(int(row[0]) for row in reader))

Detecting Dialect

csv.Sniffer().sniff(sample) detects the delimiter and quoting style automatically.
import csv, io
data = 'a|b|c\n1|2|3'
sniff = csv.Sniffer().sniff(data[:100])
print('delimiter:', sniff.delimiter)
for row in csv.reader(io.StringIO(data), dialect=sniff):
    print(row)

Quick Check

What parameter should you always set when opening a CSV file with open()?

Recap

csv.reader: rows as lists of strings. DictReader: rows as dicts. Always open with newline=''. Handle encoding for Excel files. Convert types explicitly.

Keep Going

Keep it up! Move on to the next lesson.

Frequently asked questions

Is the “Reading CSV with csv.reader” lesson free?

Yes — the full text of “Reading CSV with csv.reader” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading CSV with csv.reader”?

Read CSV files row by row and access columns by index. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading CSV with csv.reader” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. JSON Encoding and Decoding
  2. Handling Nested JSON Structures
  3. Reading CSV with csv.reader
  4. Writing CSV with csv.DictWriter
← Back to Python Academy