---
source: https://iinventedlight.com/library/notes/python-csv.md
title: "Python csv readers and writers need files opened with newline=''"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs (library)); Public-Domain (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs (library), csv — CSV File Reading and Writing; Python PEPs, PEP 305 – CSV File API. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: csv module"
tags: [python, csv, reader, writer, quoting, dialect]
client: Librarian
sources:
  - id: S1
    title: "csv — CSV File Reading and Writing"
    publisher: Python docs (library)
    url: https://docs.python.org/3/library/csv.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "PEP 305 – CSV File API"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0305/
    accessed: 2026-11-18
    license: "Public-Domain"
---
# Python csv readers and writers need files opened with newline=''
**Answer:** Open the file with `newline=''`, then use `csv.reader`/`csv.writer` (or `DictReader`/`DictWriter`). Without `newline=''`, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms with `\r\n` line endings an extra `\r` will be added on write.

## Example

```python
import csv
with open("eggs.csv", "w", newline="") as f:
    w = csv.writer(f, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL)
    w.writerow(["Spam"] * 5 + ["Baked Beans"])
with open("eggs.csv", newline="") as f:
    for row in csv.reader(f, delimiter=" ", quotechar="|"):
        print(row)
```

## Quoting constants

- `csv.QUOTE_ALL`: instructs writer objects to quote all fields.
- `csv.QUOTE_MINIMAL`: only quote those fields which contain special characters such as the delimiter or quotechar.
- The `quoting` attribute defaults to `QUOTE_MINIMAL` if quotechar is not None, and `QUOTE_NONE` otherwise.

## Inputs

The reader takes any iterable of lines, most commonly a file-like object or list, so `csv.reader(['one,two,three'])` works. An optional dialect parameter defines a set of parameters specific to a particular CSV dialect.

## Why a module

PEP 305 notes that CSV is the most common import and export format for spreadsheets and databases, yet files from different programs vary in details, which is why the module has dialects.

## See also

- [python-read-write-files](https://iinventedlight.com/library/notes/python-read-write-files/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## Claims

- id: C1
  claim: csv files should be opened with newline=''.
  source: S1
  evidence: "If csvfile is a file object, it should be opened with newline=''"

- id: C2
  claim: QUOTE_ALL quotes all fields.
  source: S1
  evidence: "Instructs writer objects to quote all fields."

- id: C3
  claim: Without newline='', embedded newlines are misread.
  source: S1
  evidence: "newlines embedded inside quoted fields will not be interpreted correctly"

- id: C4
  claim: CSV is the most common spreadsheet and database exchange format.
  source: S2
  evidence: "is the most common import and export format for spreadsheets and databases"

## Sources

- [S1] csv — CSV File Reading and Writing. Python docs (library). https://docs.python.org/3/library/csv.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] PEP 305 – CSV File API. Python PEPs. https://peps.python.org/pep-0305/ . License: Public-Domain. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs (library), csv — CSV File Reading and Writing; Python PEPs, PEP 305 – CSV File API. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
