Python csv Module

CSV (Comma-Separated Values) files are a common file format used to store tabular data.

CSV files consist of plain text, where each row represents a row of data in the table, and each column is separated by commas (or other delimiters).

CSV files are commonly used for data exchange because they are simple and easy to process.

Python provides a built-incsvmodule for reading and writing CSV files. This module simplifies the process of handling CSV files, allowing developers to easily manipulate tabular data.


1. Reading CSV Files

To read CSV files, you can use thecsv.readerobject. Here is a simple example:

Example

import csv

# Open the CSV file
with open('data.csv', mode='r', encoding='utf-8') as file:
    # Create a csv.reader object
    csv_reader = csv.reader(file)
   
    # Read data row by row
    for row in csv_reader:
        print(row)

Code Explanation:

  • open('data.csv', mode='r', encoding='utf-8'): In read-only mode, open the file nameddata.csvand specify the encoding as UTF-8.
  • csv.reader(file): Create acsv.readerobject to read the file content.
  • for row in csv_reader: Reads the file content row by row, and each row of data is parsed into a list.

2. Writing CSV Files

To write to CSV files, you can use thecsv.writerobject. Here is an example:

Example

import csv

# Data to be written
data = [
    ['Name', 'Age', 'City'],
    ['Alice', '30', 'New York'],
    ['Bob', '25', 'Los Angeles']
]

# Open the CSV file
with open('output.csv', mode='w', encoding='utf-8', newline='') as file:
    # Create a csv.writer object
    csv_writer = csv.writer(file)
   
    # Write data
    for row in data:
        csv_writer.writerow(row)

Code Explanation:

  • open('output.csv', mode='w', encoding='utf-8', newline=''): In write mode, open the file namedoutput.csvand specify the encoding as UTF-8.newline=''Used to avoid blank lines in Windows systems.
  • csv.writer(file): Create acsv.writerobject to write the file content.
  • csv_writer.writerow(row): Writes each row of data to the file.

3. Reading and Writing CSV Files Using Dictionaries

csvThe module also provides theDictReaderandDictWriterclasses, which can parse each row of a CSV file into a dictionary, or write dictionaries to a CSV file.

UsingDictReaderto read CSV files:

Example

import csv

with open('data.csv', mode='r', encoding='utf-8') as file:
    csv_dict_reader = csv.DictReader(file)
   
    for row in csv_dict_reader:
        print(row)

UsingDictWriterto write CSV files:

Example

import csv

data = [
    {'Name': 'Alice', 'Age': '30', 'City': 'New York'},
    {'Name': 'Bob', 'Age': '25', 'City': 'Los Angeles'}
]

with open('output.csv', mode='w', encoding='utf-8', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']
    csv_dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
   
    # Write the header
    csv_dict_writer.writeheader()
   
    # Write data
    for row in data:
        csv_dict_writer.writerow(row)

Common Attributes and Methods

Core Methods of the csv Module

MethodDescriptionExample
csv.reader()Read CSV data from a file objectreader = csv.reader(file)
csv.writer()Write data to a CSV filewriter = csv.writer(file)
csv.DictReader()Read CSV rows as dictionaries (with header)dict_reader = csv.DictReader(file)
csv.DictWriter()Write dictionaries to a CSV file (field names required)dict_writer = csv.DictWriter(file, fieldnames)
csv.register_dialect()Register a custom CSV format (e.g., delimiter)csv.register_dialect('mydialect', delimiter='|')
csv.unregister_dialect()Delete a registered dialectcsv.unregister_dialect('mydialect')
csv.list_dialects()List all registered dialectsprint(csv.list_dialects())

Common Methods of csv.reader and csv.writer Objects

MethodDescriptionApplicable Object
__next__()Iteratively read the next row (or use aforloop)reader
writerow(row)Write a single row of datawriter
writerows(rows)Write multiple rows of data (a list of lists)writer

Features of csv.DictReader and csv.DictWriter Objects

Feature/MethodDescriptionExample
fieldnamesField name list (DictReaderautomatically obtained from the first row)dict_reader.fieldnames
writeheader()Write the header row (DictWriterexclusive)dict_writer.writeheader()

Common Parameter Descriptions

ParameterDescriptionExample ValueApplicable Method
delimiterField delimiter','(default)'\t'reader/writer
quotecharQuote character (encloses special fields)'"'(default)reader/writer
quotingQuoting rulecsv.QUOTE_ALL(quote all)reader/writer
skipinitialspaceIgnore spaces after the delimiterTrue/Falsereader
lineterminatorLine terminator'\r\n'(default)writer
dialectPredefined dialect name'excel'(default)All methods

Example

1. Reading CSV Files

Example

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file, delimiter=',')
    for row in reader:
        print(row)  # Each row is a list

2. Writing CSV Files

Example

data = [['Name', 'Age'], ['Alice', 25], ['Bob', 30]]

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

3. Using DictReader and DictWriter (with header)

Example

# Read
with open('data.csv', 'r') as file:
    dict_reader = csv.DictReader(file)
    for row in dict_reader:
        print(row['Name'], row['Age'])  # Access via field names

# Write
fieldnames = ['Name', 'Age']
with open('output.csv', 'w', newline='') as file:
    dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
    dict_writer.writeheader()  # Write the header
    dict_writer.writerow({'Name': 'Alice', 'Age': 25})

4. Custom Dialects (e.g., handling TSV files)

Example

csv.register_dialect('tsv', delimiter='\t', quoting=csv.QUOTE_NONE)

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