Learn csv

Read and write CSV and other delimited files correctly in plain Python

12 lessons · 2 hours · 4 levels · covers csv Python 3.13

The csv module reads and writes comma-separated and other delimited text files, handling quoting, embedded commas and newlines, and spreadsheet dialects. It's the dependency-free way to import exports from Excel, databases and web services, or to produce files others can open. Knowing its rules avoids the corrupted-row bugs that come from splitting lines by hand.

Before you start

csv builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics3 lessons · 24 min in total
2. PracticalUse it productively at work, with the usual idioms6 lessons · 51 min in total
3. AdvancedThe less common features, performance and trade-offs9 lessons · 1 hour in total
4. ExpertInternals and the edge cases experts know12 lessons · 2 hours in total

The csv tree

csvReading rows with csv.readerReading rows withcsv.readerWriting rows with csv.writerWriting rows withcsv.writerDictReader and DictWriterDictReader andDictWriterConverting types and missing valuesConverting types andmissing valuesDelimiters and dialectsDelimiters anddialectsEncodings and ExcelEncodings and ExcelReading and writing filesReading and writingfilesDictionariesDictionaries
Essentials and Practical shown (6 of 12 lessons); the full list is below. Click a lesson to read it.

All 12 lessons, in order

Reading and writing

  1. Reading rows with csv.reader · Essentials · 8 min

    Open a file with newline='' and iterate csv.reader to get each row as a list of strings.

  2. Writing rows with csv.writer · Essentials · 7 min

    Write lists of values to a CSV file with csv.writer, letting it handle quoting automatically.

  3. DictReader and DictWriter · Essentials · 9 min

    Work with rows as dictionaries keyed by column name using DictReader and DictWriter.

Dialects, types and encodings

  1. Converting types and missing values · Practical · 9 min

    Turn CSV strings into ints, floats, dates and None, and know what QUOTE_NONNUMERIC does.

  2. Delimiters and dialects · Practical · 9 min

    Read and write TSV and semicolon files by setting delimiter, quotechar and lineterminator, or a named dialect.

  3. Encodings and Excel · Practical · 9 min

    Handle UTF-8 BOMs, legacy encodings and Excel's quirks when reading and writing CSV.

Robust and large-scale CSV

  1. Quoting, escaping and embedded newlines · Advanced · 11 min

    Control quoting with the QUOTE_* constants, including QUOTE_STRINGS and QUOTE_NOTNULL, and use escapechar.

  2. Detecting formats with Sniffer · Advanced · 8 min

    Guess an unknown file's delimiter, quoting and header with csv.Sniffer.

  3. Streaming, in-memory and large files · Advanced · 10 min

    Process huge CSV files row by row with generators, and read or write CSV in memory with io.StringIO.

  4. Malformed data and error handling · Expert · 12 min

    Detect and report bad rows with strict mode, csv.Error, line numbers, field_size_limit and ragged-row handling.

  5. CSV performance at scale · Expert · 11 min

    Measure and speed up CSV processing: the C parser, avoiding per-row overhead, reader vs DictReader, and when to switch tools.

  6. CSV semantics: RFC 4180 vs Python · Expert · 10 min

    Know exactly how Python's csv relates to RFC 4180 and Excel: line endings, quoting rules, whitespace and empty lines.