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
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
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
- JSON Encoding and Decoding
- Handling Nested JSON Structures
- Reading CSV with csv.reader
- Writing CSV with csv.DictWriter