The csv Module for Reading and Writing Lists

๐Ÿท๏ธ File Handling / CSV Files

๐Ÿ“Œ Context Introduction

CSV (Comma-Separated Values) files are one of the most common formats for storing tabular data. As an engineer, you will frequently encounter CSV files when dealing with logs, configuration exports, inventory lists, or data dumps from databases. Python's built-in csv module makes it incredibly simple to read from and write to these files without manually splitting strings or handling commas inside data. This guide will walk you through the basics of using the csv module to work with lists.


โš™๏ธ What is the csv Module?

The csv module is part of Python's standard library, meaning you do not need to install anything extra. It provides two primary tools: - csv.reader โ€“ for reading CSV files into lists - csv.writer โ€“ for writing lists into CSV files

The module handles edge cases automatically, such as: - Fields that contain commas (they get quoted) - Fields that contain newlines - Different delimiters (like tabs or semicolons)


๐Ÿ“Š Reading CSV Files into Lists

To read a CSV file, you first open the file using Python's built-in open() function, then pass the file object to csv.reader(). The reader returns an iterable object where each row is a list of strings.

Basic Steps: - Import the csv module: import csv - Open the file with open('filename.csv', mode='r') - Create a reader: reader = csv.reader(file_object) - Loop through the reader to access each row as a list

Example Workflow: - Suppose you have a file named servers.csv with columns: Hostname, IP Address, OS, Status - The reader will return each row as a list like ['web01', '192.168.1.10', 'Ubuntu', 'Active'] - You can access individual fields using list indexing, for example row[0] gives the hostname

Important Note: The csv.reader does not automatically skip the header row. If your CSV has a header, you can read the first row separately using next(reader) before looping through the remaining data.


๐Ÿ› ๏ธ Writing Lists to CSV Files

Writing data to a CSV file follows a similar pattern. You open a file in write mode, create a csv.writer object, and then use its methods to write rows.

Key Methods: - writerow(list) โ€“ writes a single list as one row - writerows(list_of_lists) โ€“ writes multiple rows at once

Basic Steps: - Import the csv module: import csv - Open the file with open('output.csv', mode='w', newline='') โ€“ the newline='' parameter prevents extra blank lines - Create a writer: writer = csv.writer(file_object) - Use writer.writerow() or writer.writerows() to write your data

Example Workflow: - You have a list of server records: ['db01', '192.168.1.20', 'CentOS', 'Active'] - Use writer.writerow(record) to write that single record - Or prepare a list of lists and use writer.writerows(all_records) to write everything at once


๐Ÿ•ต๏ธ Common Options and Parameters

Both csv.reader and csv.writer accept optional parameters to customize behavior:

Parameter Purpose Common Values
delimiter Character that separates fields ',' (default), '\t' (tab), ';'
quotechar Character used to quote fields containing special characters '"' (default)
quoting Controls when quotes are added csv.QUOTE_MINIMAL (default), csv.QUOTE_ALL, csv.QUOTE_NONNUMERIC
lineterminator String used to terminate rows '\n' (default), '\r\n'

Practical Example: - If your file uses tabs instead of commas, create the reader as: csv.reader(file, delimiter='\t') - If you want every field quoted when writing, use: csv.writer(file, quoting=csv.QUOTE_ALL)


๐Ÿงช Reading CSV as Dictionaries (Bonus)

Sometimes working with column names is easier than remembering index positions. The csv module also provides csv.DictReader and csv.DictWriter.

  • csv.DictReader โ€“ reads each row as an OrderedDict where keys are column headers
  • csv.DictWriter โ€“ writes dictionaries to CSV, automatically mapping keys to columns

How DictReader Works: - The first row of the CSV is automatically treated as the header - Each subsequent row is returned as a dictionary: {'Hostname': 'web01', 'IP Address': '192.168.1.10', ...} - You access values by column name: row['Hostname']

How DictWriter Works: - You must specify the fieldnames parameter (a list of column names) - Use writeheader() to write the header row - Use writerow(dictionary) to write each record


โœ… Best Practices for Engineers

  • Always use newline='' when opening files for writing CSV to avoid blank lines on Windows
  • Use with statements to ensure files are properly closed after reading or writing
  • If your data contains special characters (like non-English text), specify the encoding: open('file.csv', encoding='utf-8')
  • For large files, process rows one at a time instead of loading everything into memory
  • Test with a small sample file first to verify your delimiter and quoting settings

๐Ÿš€ Quick Reference Summary

  • Reading: csv.reader(file) returns rows as lists; csv.DictReader(file) returns rows as dictionaries
  • Writing: csv.writer(file).writerow(list) writes one row; csv.DictWriter(file, fieldnames).writerow(dict) writes dictionary rows
  • Customization: Use delimiter, quotechar, and quoting parameters to match your file format
  • Headers: For reader, handle headers manually with next(); for DictReader, headers are automatic

The csv module is a reliable, no-fuss tool that handles the messy details of CSV formatting so you can focus on processing your data. Whether you are parsing server inventories, reading log exports, or generating reports, mastering this module will save you time and prevent common parsing errors.


The csv module provides functions to read from and write to CSV (Comma-Separated Values) files, converting each row into a Python list.


๐Ÿ“˜ Example 1: Reading a CSV File into a List of Lists

This example shows how to open a CSV file and read every row as a list.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    data = list(reader)

print(data)

๐Ÿ“ค Output: [['Name', 'Age', 'City'], ['Alice', '30', 'New York'], ['Bob', '25', 'London']]


๐Ÿ“˜ Example 2: Writing a List of Lists to a CSV File

This example demonstrates writing multiple rows from a list of lists into a new CSV file.

import csv

rows = [
    ['Name', 'Age', 'City'],
    ['Alice', '30', 'New York'],
    ['Bob', '25', 'London']
]

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

print("File written successfully")

๐Ÿ“ค Output: File written successfully


๐Ÿ“˜ Example 3: Reading a CSV File Row by Row

This example shows how to process each row individually without loading the entire file into memory.

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', 'London']


๐Ÿ“˜ Example 4: Writing a Single Row to a CSV File

This example demonstrates appending one new row to an existing CSV file.

import csv

new_row = ['Charlie', '35', 'Paris']

with open('data.csv', 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(new_row)

print("Row added")

๐Ÿ“ค Output: Row added


๐Ÿ“˜ Example 5: Reading and Filtering CSV Data

This example shows how to read a CSV file and keep only rows that match a condition.

import csv

filtered_rows = []

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    header = next(reader)
    for row in reader:
        if int(row[1]) > 25:
            filtered_rows.append(row)

print(filtered_rows)

๐Ÿ“ค Output: [['Alice', '30', 'New York'], ['Charlie', '35', 'Paris']]


Comparison Table

Function Purpose Input Output
csv.reader() Read CSV file into lists File object Iterator of lists
csv.writer() Write lists to CSV file File object Writer object
writerow() Write one list as one row Single list None
writerows() Write multiple lists as rows List of lists None
next(reader) Skip header row Reader iterator First row as list

๐Ÿ“Œ Context Introduction

CSV (Comma-Separated Values) files are one of the most common formats for storing tabular data. As an engineer, you will frequently encounter CSV files when dealing with logs, configuration exports, inventory lists, or data dumps from databases. Python's built-in csv module makes it incredibly simple to read from and write to these files without manually splitting strings or handling commas inside data. This guide will walk you through the basics of using the csv module to work with lists.


โš™๏ธ What is the csv Module?

The csv module is part of Python's standard library, meaning you do not need to install anything extra. It provides two primary tools: - csv.reader โ€“ for reading CSV files into lists - csv.writer โ€“ for writing lists into CSV files

The module handles edge cases automatically, such as: - Fields that contain commas (they get quoted) - Fields that contain newlines - Different delimiters (like tabs or semicolons)


๐Ÿ“Š Reading CSV Files into Lists

To read a CSV file, you first open the file using Python's built-in open() function, then pass the file object to csv.reader(). The reader returns an iterable object where each row is a list of strings.

Basic Steps: - Import the csv module: import csv - Open the file with open('filename.csv', mode='r') - Create a reader: reader = csv.reader(file_object) - Loop through the reader to access each row as a list

Example Workflow: - Suppose you have a file named servers.csv with columns: Hostname, IP Address, OS, Status - The reader will return each row as a list like ['web01', '192.168.1.10', 'Ubuntu', 'Active'] - You can access individual fields using list indexing, for example row[0] gives the hostname

Important Note: The csv.reader does not automatically skip the header row. If your CSV has a header, you can read the first row separately using next(reader) before looping through the remaining data.


๐Ÿ› ๏ธ Writing Lists to CSV Files

Writing data to a CSV file follows a similar pattern. You open a file in write mode, create a csv.writer object, and then use its methods to write rows.

Key Methods: - writerow(list) โ€“ writes a single list as one row - writerows(list_of_lists) โ€“ writes multiple rows at once

Basic Steps: - Import the csv module: import csv - Open the file with open('output.csv', mode='w', newline='') โ€“ the newline='' parameter prevents extra blank lines - Create a writer: writer = csv.writer(file_object) - Use writer.writerow() or writer.writerows() to write your data

Example Workflow: - You have a list of server records: ['db01', '192.168.1.20', 'CentOS', 'Active'] - Use writer.writerow(record) to write that single record - Or prepare a list of lists and use writer.writerows(all_records) to write everything at once


๐Ÿ•ต๏ธ Common Options and Parameters

Both csv.reader and csv.writer accept optional parameters to customize behavior:

Parameter Purpose Common Values
delimiter Character that separates fields ',' (default), '\t' (tab), ';'
quotechar Character used to quote fields containing special characters '"' (default)
quoting Controls when quotes are added csv.QUOTE_MINIMAL (default), csv.QUOTE_ALL, csv.QUOTE_NONNUMERIC
lineterminator String used to terminate rows '\n' (default), '\r\n'

Practical Example: - If your file uses tabs instead of commas, create the reader as: csv.reader(file, delimiter='\t') - If you want every field quoted when writing, use: csv.writer(file, quoting=csv.QUOTE_ALL)


๐Ÿงช Reading CSV as Dictionaries (Bonus)

Sometimes working with column names is easier than remembering index positions. The csv module also provides csv.DictReader and csv.DictWriter.

  • csv.DictReader โ€“ reads each row as an OrderedDict where keys are column headers
  • csv.DictWriter โ€“ writes dictionaries to CSV, automatically mapping keys to columns

How DictReader Works: - The first row of the CSV is automatically treated as the header - Each subsequent row is returned as a dictionary: {'Hostname': 'web01', 'IP Address': '192.168.1.10', ...} - You access values by column name: row['Hostname']

How DictWriter Works: - You must specify the fieldnames parameter (a list of column names) - Use writeheader() to write the header row - Use writerow(dictionary) to write each record


โœ… Best Practices for Engineers

  • Always use newline='' when opening files for writing CSV to avoid blank lines on Windows
  • Use with statements to ensure files are properly closed after reading or writing
  • If your data contains special characters (like non-English text), specify the encoding: open('file.csv', encoding='utf-8')
  • For large files, process rows one at a time instead of loading everything into memory
  • Test with a small sample file first to verify your delimiter and quoting settings

๐Ÿš€ Quick Reference Summary

  • Reading: csv.reader(file) returns rows as lists; csv.DictReader(file) returns rows as dictionaries
  • Writing: csv.writer(file).writerow(list) writes one row; csv.DictWriter(file, fieldnames).writerow(dict) writes dictionary rows
  • Customization: Use delimiter, quotechar, and quoting parameters to match your file format
  • Headers: For reader, handle headers manually with next(); for DictReader, headers are automatic

The csv module is a reliable, no-fuss tool that handles the messy details of CSV formatting so you can focus on processing your data. Whether you are parsing server inventories, reading log exports, or generating reports, mastering this module will save you time and prevent common parsing errors.

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 functions to read from and write to CSV (Comma-Separated Values) files, converting each row into a Python list.


๐Ÿ“˜ Example 1: Reading a CSV File into a List of Lists

This example shows how to open a CSV file and read every row as a list.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    data = list(reader)

print(data)

๐Ÿ“ค Output: [['Name', 'Age', 'City'], ['Alice', '30', 'New York'], ['Bob', '25', 'London']]


๐Ÿ“˜ Example 2: Writing a List of Lists to a CSV File

This example demonstrates writing multiple rows from a list of lists into a new CSV file.

import csv

rows = [
    ['Name', 'Age', 'City'],
    ['Alice', '30', 'New York'],
    ['Bob', '25', 'London']
]

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

print("File written successfully")

๐Ÿ“ค Output: File written successfully


๐Ÿ“˜ Example 3: Reading a CSV File Row by Row

This example shows how to process each row individually without loading the entire file into memory.

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', 'London']


๐Ÿ“˜ Example 4: Writing a Single Row to a CSV File

This example demonstrates appending one new row to an existing CSV file.

import csv

new_row = ['Charlie', '35', 'Paris']

with open('data.csv', 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(new_row)

print("Row added")

๐Ÿ“ค Output: Row added


๐Ÿ“˜ Example 5: Reading and Filtering CSV Data

This example shows how to read a CSV file and keep only rows that match a condition.

import csv

filtered_rows = []

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    header = next(reader)
    for row in reader:
        if int(row[1]) > 25:
            filtered_rows.append(row)

print(filtered_rows)

๐Ÿ“ค Output: [['Alice', '30', 'New York'], ['Charlie', '35', 'Paris']]


Comparison Table

Function Purpose Input Output
csv.reader() Read CSV file into lists File object Iterator of lists
csv.writer() Write lists to CSV file File object Writer object
writerow() Write one list as one row Single list None
writerows() Write multiple lists as rows List of lists None
next(reader) Skip header row Reader iterator First row as list