Course Outline (Part 36)

A CSV (Comma Separated Values) file is a type of plain text file that uses specific structuring to arrange tabular data. Because it’s a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters.

Python provides a built-in csv module to handle reading and writing CSV files.


1. csv module

To work with CSV files, you first need to import the csv module.

import csv

2. csv.reader()

The csv.reader object returns an iterable that will process lines from a CSV file. Each row returned by the reader is a list of strings.

Suppose we have a file called employees.csv:

Name,Department,Birthday Month
John Smith,Accounting,November
Erica Meyers,IT,March

You can read it like this:

import csv

with open('employees.csv', mode='r') as file:
    csv_reader = csv.reader(file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are: {", ".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')

3. csv.writer()

To write to a CSV file, use the csv.writer object.

import csv

with open('employee_file.csv', mode='w', newline='') as file:
    employee_writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    # Write headers
    employee_writer.writerow(['John Smith', 'Accounting', 'November'])
    employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

Note: newline='' is necessary to prevent extra blank lines from being added in Windows.


4. csv.DictReader()

Reading a CSV file directly into a dictionary makes the data much easier to work with, especially if you have many columns, because you can reference the data by the column header name rather than an index.

import csv

with open('employees.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        print(f'{row["Name"]} works in the {row["Department"]} department.')

5. csv.DictWriter()

To write dictionaries to a CSV file, use csv.DictWriter. You must define the fieldnames beforehand so the writer knows what the headers should be.

import csv

with open('employee_file.csv', mode='w', newline='') as csv_file:
    fieldnames = ['emp_name', 'dept', 'birth_month']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

    # Write the header row
    writer.writeheader()
    
    # Write the data rows
    writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting', 'birth_month': 'November'})
    writer.writerow({'emp_name': 'Erica Meyers', 'dept': 'IT', 'birth_month': 'March'})

Discussion

Loading comments...