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. This is 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, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.
Introduction
Opening a CSV File
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
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
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
import csv, io
data = 'a;b;c\n1;2;3'
for row in csv.reader(io.StringIO(data), delimiter=';'):
print(row)Handling Quoted Fields
import csv, io
data = 'name,note\n"Smith, John","great guy"'
for row in csv.reader(io.StringIO(data)):
print(row)csv.DictReader
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
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
# with open('data.csv', encoding='utf-8-sig', newline='') as f:
# reader = csv.DictReader(f)
print('encoding demo')Large CSV Files
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
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
Recap
Keep Going
Learn Python with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 76
- Lessons
- 320
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. 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. The Python Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- JSON Encoding and Decoding
- Handling Nested JSON Structures
- Reading CSV with csv.reader
- Writing CSV with csv.DictWriter