Using csv.reader and csv.writer

🏷️ File Handling / CSV Files

📝 Context Introduction

CSV (Comma-Separated Values) files are one of the most common formats for storing tabular data in infrastructure work. Whether you're processing server logs, configuration exports, or inventory lists, you'll frequently encounter CSV files. Python's built-in csv module provides two powerful tools: csv.reader for reading data and csv.writer for writing data. These tools handle the complexities of parsing commas, quotes, and newlines automatically, making your life much easier.


⚙️ What Are csv.reader and csv.writer?

  • csv.reader – Reads a CSV file and returns each row as a list of strings
  • csv.writer – Writes lists of data into a CSV file, handling formatting automatically
  • Both are part of Python's csv module, which you import with import csv
  • They work with file objects opened using Python's open() function
  • The reader and writer handle edge cases like commas inside quoted fields

📊 Reading CSV Files with csv.reader

Basic usage pattern: - Open the file using open('filename.csv', 'r') with mode 'r' for reading - Pass the file object to csv.reader(file_object) - Loop through the reader to access each row as a list - Close the file using file.close() or use a with statement for automatic cleanup

Example workflow: - Create a file object: file = open('servers.csv', 'r') - Create a reader: reader = csv.reader(file) - Loop through rows: for row in reader: print(row) - Close the file: file.close()

Using the with statement (recommended): - with open('servers.csv', 'r') as file: - Inside the block: reader = csv.reader(file) - Then: for row in reader: print(row) - The file closes automatically when the block ends

Accessing specific columns: - Each row is a list, so you use index numbers: row[0] for first column, row[1] for second, etc. - Example: print(f"Server name: {row[0]}, IP: {row[1]}")

Skipping header rows: - Use next(reader) before the loop to skip the first row (header) - Example: header = next(reader) then loop through the remaining rows


🛠️ Writing CSV Files with csv.writer

Basic usage pattern: - Open the file using open('output.csv', 'w', newline='') with mode 'w' for writing - The newline='' parameter prevents extra blank lines on Windows - Pass the file object to csv.writer(file_object) - Use writer.writerow(list) to write a single row - Use writer.writerows(list_of_lists) to write multiple rows at once

Example workflow: - Create a file object: file = open('output.csv', 'w', newline='') - Create a writer: writer = csv.writer(file) - Write header: writer.writerow(['Name', 'IP', 'OS']) - Write data: writer.writerow(['web01', '10.0.0.1', 'Ubuntu']) - Close the file: file.close()

Using the with statement (recommended): - with open('output.csv', 'w', newline='') as file: - Inside the block: writer = csv.writer(file) - Then: writer.writerow(['Name', 'IP', 'OS']) - The file closes automatically when the block ends

Writing multiple rows at once: - Prepare a list of lists: data = [['web01', '10.0.0.1'], ['db01', '10.0.0.2']] - Use: writer.writerows(data)


🕵️ Common CSV Dialects and Options

What is a dialect? - A dialect defines the format rules for a CSV file (delimiter, quote character, etc.) - The default dialect is excel, which uses commas and double quotes

Common parameters you can customize: - delimiter – The character separating values (default is comma) - quotechar – The character used to quote fields (default is double quote) - quoting – Controls when quotes are added (use csv.QUOTE_ALL or csv.QUOTE_MINIMAL) - lineterminator – The line ending character (default is \r\n)

Example with custom delimiter (tab-separated): - reader = csv.reader(file, delimiter='\t') - writer = csv.writer(file, delimiter='\t')

Example with all quotes: - writer = csv.writer(file, quoting=csv.QUOTE_ALL)


📊 Comparison: csv.reader vs csv.writer

Feature csv.reader csv.writer
Purpose Read CSV data into Python Write Python data to CSV
Input/Output Takes a file object, returns rows Takes a file object, writes rows
Row format Returns each row as a list Accepts each row as a list
Method Loop through the reader object Use writerow() or writerows()
File mode 'r' for reading 'w' for writing (or 'a' for appending)
Newline handling Handled automatically Use newline='' when opening file

💡 Practical Tips for Engineers

  • Always use the with statement to avoid forgetting to close files
  • Use newline='' when opening files for writing CSV on Windows
  • Check if a file exists before reading using os.path.exists('filename.csv')
  • For large files, process rows one at a time instead of loading everything into memory
  • Use try/except blocks to handle file not found or permission errors
  • Test your CSV output by opening it in a spreadsheet application
  • Remember that csv.reader returns strings – convert to numbers with int() or float() when needed

✅ Summary

  • csv.reader and csv.writer are your go-to tools for handling CSV files in Python
  • Always import the csv module first: import csv
  • Use the with statement for safe file handling
  • Customize delimiters and quoting options as needed for different CSV formats
  • These tools handle all the tricky formatting details so you can focus on your data

Now you're ready to read and write CSV files like a pro!


The csv module provides reader and writer functions to read from and write to comma-separated value files.


📘 Example 1: Reading a CSV file with csv.reader

This example shows how to open and read all rows from a CSV file.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

📤 Output: ['Name', 'Age', 'City'] ['Alice', '30', 'New York'] ['Bob', '25', 'Los Angeles']


📘 Example 2: Writing a CSV file with csv.writer

This example shows how to create a new CSV file and write rows to it.

import csv

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Score'])
    writer.writerow(['Alice', 95])
    writer.writerow(['Bob', 87])

📤 Output: (file 'output.csv' created with three rows)


📘 Example 3: Reading specific columns from a CSV file

This example shows how to access individual columns by index from each row.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    next(reader)  # skip header row
    for row in reader:
        name = row[0]
        age = row[1]
        print(f"{name} is {age} years old")

📤 Output: Alice is 30 years old Bob is 25 years old


📘 Example 4: Writing multiple rows at once with writerows

This example shows how to write a list of rows in a single operation.

import csv

data = [
    ['Product', 'Price', 'Quantity'],
    ['Widget', 12.50, 100],
    ['Gadget', 24.99, 50],
    ['Doohickey', 5.75, 200]
]

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

📤 Output: (file 'inventory.csv' created with four rows)


📘 Example 5: Reading a CSV and writing a filtered version

This example shows how to read data from one CSV file and write only selected rows to another.

import csv

with open('data.csv', 'r') as infile:
    reader = csv.reader(infile)
    with open('filtered.csv', 'w', newline='') as outfile:
        writer = csv.writer(outfile)
        header = next(reader)
        writer.writerow(header)
        for row in reader:
            age = int(row[1])
            if age > 25:
                writer.writerow(row)

📤 Output: (file 'filtered.csv' created with only rows where age > 25)


Quick Comparison

Feature csv.reader csv.writer
Purpose Reads data from CSV files Writes data to CSV files
Returns An iterable of row lists A writer object with write methods
Common methods next() to skip header writerow() for one row, writerows() for many
File mode 'r' (read) 'w' (write) with newline=''
Handles headers Manually skip with next() Manually write header row first

📝 Context Introduction

CSV (Comma-Separated Values) files are one of the most common formats for storing tabular data in infrastructure work. Whether you're processing server logs, configuration exports, or inventory lists, you'll frequently encounter CSV files. Python's built-in csv module provides two powerful tools: csv.reader for reading data and csv.writer for writing data. These tools handle the complexities of parsing commas, quotes, and newlines automatically, making your life much easier.


⚙️ What Are csv.reader and csv.writer?

  • csv.reader – Reads a CSV file and returns each row as a list of strings
  • csv.writer – Writes lists of data into a CSV file, handling formatting automatically
  • Both are part of Python's csv module, which you import with import csv
  • They work with file objects opened using Python's open() function
  • The reader and writer handle edge cases like commas inside quoted fields

📊 Reading CSV Files with csv.reader

Basic usage pattern: - Open the file using open('filename.csv', 'r') with mode 'r' for reading - Pass the file object to csv.reader(file_object) - Loop through the reader to access each row as a list - Close the file using file.close() or use a with statement for automatic cleanup

Example workflow: - Create a file object: file = open('servers.csv', 'r') - Create a reader: reader = csv.reader(file) - Loop through rows: for row in reader: print(row) - Close the file: file.close()

Using the with statement (recommended): - with open('servers.csv', 'r') as file: - Inside the block: reader = csv.reader(file) - Then: for row in reader: print(row) - The file closes automatically when the block ends

Accessing specific columns: - Each row is a list, so you use index numbers: row[0] for first column, row[1] for second, etc. - Example: print(f"Server name: {row[0]}, IP: {row[1]}")

Skipping header rows: - Use next(reader) before the loop to skip the first row (header) - Example: header = next(reader) then loop through the remaining rows


🛠️ Writing CSV Files with csv.writer

Basic usage pattern: - Open the file using open('output.csv', 'w', newline='') with mode 'w' for writing - The newline='' parameter prevents extra blank lines on Windows - Pass the file object to csv.writer(file_object) - Use writer.writerow(list) to write a single row - Use writer.writerows(list_of_lists) to write multiple rows at once

Example workflow: - Create a file object: file = open('output.csv', 'w', newline='') - Create a writer: writer = csv.writer(file) - Write header: writer.writerow(['Name', 'IP', 'OS']) - Write data: writer.writerow(['web01', '10.0.0.1', 'Ubuntu']) - Close the file: file.close()

Using the with statement (recommended): - with open('output.csv', 'w', newline='') as file: - Inside the block: writer = csv.writer(file) - Then: writer.writerow(['Name', 'IP', 'OS']) - The file closes automatically when the block ends

Writing multiple rows at once: - Prepare a list of lists: data = [['web01', '10.0.0.1'], ['db01', '10.0.0.2']] - Use: writer.writerows(data)


🕵️ Common CSV Dialects and Options

What is a dialect? - A dialect defines the format rules for a CSV file (delimiter, quote character, etc.) - The default dialect is excel, which uses commas and double quotes

Common parameters you can customize: - delimiter – The character separating values (default is comma) - quotechar – The character used to quote fields (default is double quote) - quoting – Controls when quotes are added (use csv.QUOTE_ALL or csv.QUOTE_MINIMAL) - lineterminator – The line ending character (default is \r\n)

Example with custom delimiter (tab-separated): - reader = csv.reader(file, delimiter='\t') - writer = csv.writer(file, delimiter='\t')

Example with all quotes: - writer = csv.writer(file, quoting=csv.QUOTE_ALL)


📊 Comparison: csv.reader vs csv.writer

Feature csv.reader csv.writer
Purpose Read CSV data into Python Write Python data to CSV
Input/Output Takes a file object, returns rows Takes a file object, writes rows
Row format Returns each row as a list Accepts each row as a list
Method Loop through the reader object Use writerow() or writerows()
File mode 'r' for reading 'w' for writing (or 'a' for appending)
Newline handling Handled automatically Use newline='' when opening file

💡 Practical Tips for Engineers

  • Always use the with statement to avoid forgetting to close files
  • Use newline='' when opening files for writing CSV on Windows
  • Check if a file exists before reading using os.path.exists('filename.csv')
  • For large files, process rows one at a time instead of loading everything into memory
  • Use try/except blocks to handle file not found or permission errors
  • Test your CSV output by opening it in a spreadsheet application
  • Remember that csv.reader returns strings – convert to numbers with int() or float() when needed

✅ Summary

  • csv.reader and csv.writer are your go-to tools for handling CSV files in Python
  • Always import the csv module first: import csv
  • Use the with statement for safe file handling
  • Customize delimiters and quoting options as needed for different CSV formats
  • These tools handle all the tricky formatting details so you can focus on your data

Now you're ready to read and write CSV files like a pro!

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

The csv module provides reader and writer functions to read from and write to comma-separated value files.


📘 Example 1: Reading a CSV file with csv.reader

This example shows how to open and read all rows from a CSV file.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

📤 Output: ['Name', 'Age', 'City'] ['Alice', '30', 'New York'] ['Bob', '25', 'Los Angeles']


📘 Example 2: Writing a CSV file with csv.writer

This example shows how to create a new CSV file and write rows to it.

import csv

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Score'])
    writer.writerow(['Alice', 95])
    writer.writerow(['Bob', 87])

📤 Output: (file 'output.csv' created with three rows)


📘 Example 3: Reading specific columns from a CSV file

This example shows how to access individual columns by index from each row.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    next(reader)  # skip header row
    for row in reader:
        name = row[0]
        age = row[1]
        print(f"{name} is {age} years old")

📤 Output: Alice is 30 years old Bob is 25 years old


📘 Example 4: Writing multiple rows at once with writerows

This example shows how to write a list of rows in a single operation.

import csv

data = [
    ['Product', 'Price', 'Quantity'],
    ['Widget', 12.50, 100],
    ['Gadget', 24.99, 50],
    ['Doohickey', 5.75, 200]
]

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

📤 Output: (file 'inventory.csv' created with four rows)


📘 Example 5: Reading a CSV and writing a filtered version

This example shows how to read data from one CSV file and write only selected rows to another.

import csv

with open('data.csv', 'r') as infile:
    reader = csv.reader(infile)
    with open('filtered.csv', 'w', newline='') as outfile:
        writer = csv.writer(outfile)
        header = next(reader)
        writer.writerow(header)
        for row in reader:
            age = int(row[1])
            if age > 25:
                writer.writerow(row)

📤 Output: (file 'filtered.csv' created with only rows where age > 25)


Quick Comparison

Feature csv.reader csv.writer
Purpose Reads data from CSV files Writes data to CSV files
Returns An iterable of row lists A writer object with write methods
Common methods next() to skip header writerow() for one row, writerows() for many
File mode 'r' (read) 'w' (write) with newline=''
Handles headers Manually skip with next() Manually write header row first