Python CSV — Reading & Writing Tabular Data

Python Standard LibraryCSVFree Lesson

Advertisement

Python CSV — Reading & Writing Tabular Data

CSV (Comma-Separated Values) is the most common format for tabular data exchange.

Learning Objectives

  • Read and write CSV files with the csv module
  • Handle different delimiters and quoting styles
  • Process large CSV files efficiently
  • Use csv.DictReader for named columns

Reading CSV

import csv

# Basic reading
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    header = next(reader)  # First row as header
    for row in reader:
        print(row)  # Each row is a list of strings

# DictReader — columns accessed by name
with open('data.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row['name'], row['age'])

Writing CSV

import csv

# Basic writing
data = [
    ["Name", "Age", "City"],
    ["Alice", 30, "NYC"],
    ["Bob", 25, "LA"],
    ["Charlie", 35, "Chicago"]
]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(data)

# DictWriter — write from dicts
with open('output.csv', 'w', newline='') as f:
    fieldnames = ['name', 'age', 'city']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'name': 'Alice', 'age': 30, 'city': 'NYC'})

Different Delimiters

# Tab-separated
with open('data.tsv', 'r') as f:
    reader = csv.reader(f, delimiter='\t')

# Semicolon-separated (common in Europe)
with open('data.csv', 'r') as f:
    reader = csv.reader(f, delimiter=';')

Processing Large Files

import csv

def process_large_csv(filename):
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        total = 0
        count = 0
        for row in reader:
            total += float(row['amount'])
            count += 1
        return total / count if count else 0

Key Takeaways

  1. Use newline='' when opening CSV files in Python
  2. DictReader and DictWriter for named column access
  3. Always specify delimiter for non-comma separators
  4. Process row-by-row for large files (memory efficient)
  5. CSV module handles quoting and escaping automatically

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement