Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does Python’s CSV writer handle commas, quotes, and line breaks inside fields?

Quick answer

Python’s csv.writer handles these characters using CSV quoting rules. With its usual Excel dialect and default QUOTE_MINIMAL setting, it quotes a field when it contains a delimiter, quote character, or newline, and doubles quote characters inside a quoted field. [S1] Write rows with the CSV module rather than joining values with commas, which does not apply those rules. When writing to a file, open it with newline=''. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does Python’s CSV writer do when a field contains a comma, a quote, or a line break? I want to save rows without accidentally splitting one value into several columns.

Geez

Use csv.writer to write each row as a sequence of field values. With Python’s usual Excel dialect, the default QUOTE_MINIMAL behavior adds quotes when a field contains the delimiter (normally a comma), a quote character, or a newline. A quote inside a quoted field is doubled. [S1]

For example, these are three individual field values:

["likes tea, coffee", 'said "hello"', "first line\nsecond line"]

The CSV text for those fields uses quotes around each value. The embedded quote becomes two quote characters, while the line break remains part of its quoted field. The line break can make that one CSV record appear on more than one screen line; it does not mean the field has become two separate fields.

Follow-up question

Can you show a complete example with the input rows and the resulting CSV text?

Geez

Here is a small, harmless example. The first row is a header; the next three rows demonstrate a comma, an embedded quote, and a line break.

import csv

rows = [
    ["name", "note"],
    ["Mira", "likes tea, coffee"],
    ["Sam", 'said "hello"'],
    ["Lee", "first line\nsecond line"],
]

with open("example.csv", "w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(rows)

Opening the file with newline='' is the documented recommendation for writing to a file. [S1] A readable rendering of the resulting CSV contents is:

name,note
Mira,"likes tea, coffee"
Sam,"said ""hello"""
Lee,"first line
second line"

In the last record, first line and second line are displayed on separate physical lines, but both belong to the quoted note field. This shows the default behavior for the example; changing the CSV dialect or writer settings can change the conventions used. [S1]

Follow-up question

Why can’t I just join each row with commas? It seems like that would produce the same output for ordinary values.

Geez

Joining can appear to work when values contain no special characters, but it does not encode CSV’s quoting and escaping rules. [S1] For example, joining ['Mira', 'likes tea, coffee'] produces Mira,likes tea, coffee. A reader that treats commas as delimiters can interpret that as three fields rather than two.

Joining also leaves an embedded quote unescaped and does not mark an embedded line break as part of one field. The CSV writer applies these rules for you, so it is the safer choice even if some rows currently contain only simple values. Provide it the values as a sequence; do not pre-join each row.

Follow-up question

How can I check the output when I’m debugging, especially if a field contains a line break?

Geez

Inspect the CSV text and check that a field with a comma is surrounded by quotes, an embedded quote is doubled, and a multiline field is quoted as one field. A line break inside a quoted field can make the record span more than one physical line in the displayed text.

These checks illustrate Python’s documented default behavior; they do not establish that every spreadsheet or other CSV application uses identical conventions. [S1] If another application expects a different delimiter or dialect, confirm its requirements and configure the writer accordingly.

A practical checklist

  • Pass each row to writer.writerow() or writer.writerows() as a sequence of field values.

  • Open an output file with newline='' when using Python’s CSV writer.

  • Check that comma-containing values are quoted in the resulting text.

  • Check that embedded quotes are doubled and line breaks remain inside a quoted field.

  • Confirm the target application’s expected CSV dialect if it requires conventions different from Python’s defaults.

A few more useful answers

Does the CSV writer quote every field?

Not with the usual Excel dialect’s default QUOTE_MINIMAL setting: it quotes fields that need quoting because they contain the delimiter, a quote character, or a newline. [S1]

Does a line break inside a quoted field create a new CSV record?

Not by itself: a line break can be part of a quoted field, as in the example. The displayed text may span multiple physical lines even though it represents one field.

Will every program interpret this CSV the same way?

Not necessarily. Python’s documented defaults describe its usual Excel dialect, not every application’s conventions; check the receiving application’s dialect requirements. [S1]

Sources

References used for this guide.

  1. [S1] csv — CSV File Reading and Writing (Python 3.14 documentation)
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work