Reading CSV with csv.reader
Read CSV files row by row and access columns by index.
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)All lessons in this course
- JSON Encoding and Decoding
- Handling Nested JSON Structures
- Reading CSV with csv.reader
- Writing CSV with csv.DictWriter