Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

csv

Python’s csv module reads and writes tabular data in CSV format, supporting both sequence rows and dictionary rows. Dialects and formatting parameters adapt it to different CSV conventions.

How it works

The csv module converts between CSV text and Python sequences or dictionaries. Use reader and writer for positional rows, or DictReader and DictWriter when column names should map to dictionary keys; dialect and formatting options control how fields are separated and quoted.

Syntax

csv.reader(csvfile, dialect='excel', **fmtparams)
csv.writer(csvfile, dialect='excel', **fmtparams)
csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)

Parameters

NameTypeDescription
csvfileiterableInput iterable or file-like object containing CSV lines.
dialectstr or DialectOptional dialect name or dialect object controlling CSV formatting.
fmtparamskeyword argumentsAdditional formatting controls such as delimiter, quotechar, quoting, and lineterminator.

Returns

reader() returns a reader object; writer() returns a writer object; DictReader yields dictionaries; DictWriter writes dictionary rows.

Key points

  • reader and writer provide sequence-based row processing.
  • DictReader and DictWriter support column-oriented dictionary workflows.
  • csv.reader creates an iterator-like reader over the supplied input.
  • csv.writer serializes supplied row data into delimited output.
  • DictReader turns each input row into a dictionary keyed by field names.

Common mistakes

  • Open file objects with newline='' when passing them to the CSV reader or writer so newline handling remains correct.
  • When writing rows, a None value becomes an empty field and cannot be distinguished from an originally empty string.

Example: Read and rewrite CSV rows

import csv
from io import StringIO

source = StringIO("name,age\nAda,36\nGrace,28\n")
rows = csv.DictReader(source)

output = StringIO()
writer = csv.DictWriter(output, fieldnames=["name", "age"])
writer.writeheader()
writer.writerows(rows)
print(output.getvalue(), end="")

Reads CSV rows as dictionaries, then writes them with an explicit header.

When to use it

CSV ingestion

Import records from spreadsheet exports into backend or scripting workflows.

CSV export

Generate files compatible with spreadsheet applications and other CSV consumers.

Named-column transformation

Transform rows while retaining named columns for clearer application logic.

Interview questions

QWhich csv APIs handle positional rows versus named columns?

Use csv.reader for rows represented as sequences and csv.writer to emit sequences. Use DictReader and DictWriter when fields should be addressed by column names.

Follow-up: How would you preserve a supplied schema when writing? Pass fieldnames to DictWriter and call writeheader() before writerows().

QWhat do reader and writer produce or consume?

A reader consumes lines from a CSV input and returns each row as a list of strings by default. A writer accepts row sequences and serializes them according to its dialect and formatting settings.

Follow-up: How can a reader use a nonstandard separator? Supply a dialect or a delimiter formatting parameter when constructing it.

QHow do DictReader and DictWriter determine column names?

DictReader maps values in each row to dictionary keys from fieldnames; if fieldnames are omitted, the first row supplies them. DictWriter maps dictionary keys to output columns named by its fieldnames argument.

Follow-up: What happens with dictionary keys that are not output fields? Configure the extrasaction option to control whether they are ignored or rejected.

  • DictReader
  • DictWriter
  • Dialect
  • Sniffer
  • reader
  • writer
  • register_dialect
  • get_dialect

Source: csv — CSV File Reading and Writing. Checked 2026-10-16.