Skip to content

gh-75008: Detect the lineterminator in csv.Sniffer.sniff() - #155061

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:csv-sniffer-lineterminator
Aug 13, 2026
Merged

gh-75008: Detect the lineterminator in csv.Sniffer.sniff()#155061
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:csv-sniffer-lineterminator

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 1, 2026

Copy link
Copy Markdown
Member

csv.Sniffer.sniff() always returned lineterminator='\r\n'. It is now guessed by a majority vote among the line endings of the sample, which the line splitter already preserves. A tie is broken in the order '\r\n', '\n', '\r', so a sample without a complete line still gives '\r\n'.

A line break inside a quoted field is counted too, because the vote is taken over the raw lines of the sample. It only wins if such breaks outnumber the real ones. Excluding them is possible on top of this (record which lines the reader pulls for each row), but it is not needed for the majority vote.

This only affects the writer: the reader recognizes '\r' and '\n' as end-of-line and ignores lineterminator.

PR #18336 addresses the same issue against the old sniffer, which was replaced by trial parsing in gh-83273.

It is guessed by a majority vote among the line endings of the sample,
instead of always being '\r\n'.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33870308 | 📁 Comparing 9d1a329 against main (7b4165b)

  🔍 Preview build  

3 files changed
± library/csv.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) August 13, 2026 10:08
@serhiy-storchaka
serhiy-storchaka merged commit 3157786 into python:main Aug 13, 2026
98 of 100 checks passed
mbeijen pushed a commit to mbeijen/cpython that referenced this pull request Aug 14, 2026
…honGH-155061)

It is guessed by a majority vote among the line endings of the sample,
instead of always being '\r\n'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant