FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-75008: Detect the lineterminator in csv.Sniffer.sniff() by serhiy-storchaka · Pull Request #155061 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 6 additions & 1 deletion Doc/library/csv.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,15 @@ The :mod:`!csv` module defines the following classes:
are preferred, in this order,
no matter how many times each of them occurs.

The *lineterminator* parameter is deduced separately,
by a majority vote among the line endings of the sample.
A tie is broken in the order ``'\r\n'``, ``'\n'``, ``'\r'``,
so a sample without a complete line gives ``'\r\n'``.

.. versionchanged:: next
The dialect is now deduced by trial parsing
and the results may differ from those of earlier Python versions.
The *escapechar* parameter can now be detected,
The *escapechar* and *lineterminator* parameters can now be detected,
and the requested *delimiters* are not restricted to ASCII.


Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.16.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ csv
The results may differ from those of earlier Python versions.
(Contributed by Serhiy Storchaka in :gh:`83273`.)

* :meth:`csv.Sniffer.sniff` now detects the *lineterminator* parameter
by a majority vote among the line endings of the sample,
instead of always returning ``'\r\n'``.
(Contributed by Serhiy Storchaka in :gh:`75008`.)

curses
------

Expand Down
20 changes: 19 additions & 1 deletion Lib/csv.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ def sniff(self, sample, delimiters=None):

class dialect(Dialect):
_name = "sniffed"
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL

dialect.lineterminator = self._detect_lineterminator(lines)
dialect.delimiter = delimiter
# _csv.reader won't accept a quotechar of ''
dialect.quotechar = quotechar or '"'
Expand Down Expand Up @@ -598,6 +598,24 @@ def _detect_skipinitialspace(self, lines, delimiter, quotechar,
for kept_row, skipped_row in zip(*results)]
return all(first) or not any(first)

def _detect_lineterminator(self, lines):
"""
Detect the line terminator by majority vote among the line
endings. A line break inside a quoted field is counted too,
but it takes more of them than of the real ones to win the
vote. A tie is broken in the order '\\r\\n', '\\n', '\\r',
so a sample without a complete line gives '\\r\\n'.
"""
counts = dict.fromkeys(('\r\n', '\n', '\r'), 0)
for line in lines:
for lineterminator in counts:
if line.endswith(lineterminator):
counts[lineterminator] += 1
break
# max() returns the first of equal candidates, and dict
# preserves the insertion order.
return max(counts, key=counts.get)

def has_header(self, sample):
# Creates a dictionary of types of data in each column. If any
# column is of a single type (say, integers), *except* for the first
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_csv.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,39 @@ def test_sniff_crlf_lineterminator(self):
dialect = sniffer.sniff(sample)
self.assertEqual(dialect.delimiter, ',')
self.assertEqual(dialect.quotechar, '"')
self.assertEqual(dialect.lineterminator, '\r\n')

def test_sniff_lineterminator(self):
sniffer = csv.Sniffer()
for lineterminator in '\r\n', '\n', '\r':
with self.subTest(lineterminator=lineterminator):
sample = lineterminator.join(['a,b,c', 'd,e,f', 'g,h,i', ''])
dialect = sniffer.sniff(sample)
self.assertEqual(dialect.lineterminator, lineterminator)
self.assertEqual(dialect.delimiter, ',')
# The majority wins.
sample = 'a,b,c\nd,e,f\r\ng,h,i\n'
self.assertEqual(sniffer.sniff(sample).lineterminator, '\n')
sample = 'a,b,c\r\nd,e,f\ng,h,i\r\n'
self.assertEqual(sniffer.sniff(sample).lineterminator, '\r\n')
# A line break inside a quoted field is counted too, but it is
# outvoted by the real ones.
sample = 'a,"x\ny",c\r\nd,e,f\r\ng,h,i\r\n'
self.assertEqual(sniffer.sniff(sample).lineterminator, '\r\n')

def test_sniff_lineterminator_tie(self):
# A tie is broken in the order '\r\n', '\n', '\r'.
sniffer = csv.Sniffer()
for sample, lineterminator in (
('a,b,c\nd,e,f\r\n', '\r\n'),
('a,b,c\r\nd,e,f\ng,h,i\rj,k,l', '\r\n'),
('a,b,c\nd,e,f\rg,h,i', '\n'),
# A sample without a complete line is a tie of zeros.
('a,b,c', '\r\n'),
):
with self.subTest(sample=sample):
self.assertEqual(sniffer.sniff(sample).lineterminator,
lineterminator)

def test_sniff_excel_tab_with_quotes(self):
# gh-62029: tab-delimited data with a quoted field containing
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`csv.Sniffer.sniff` now detects the *lineterminator* parameter by a
majority vote among the line endings of the sample.
Loading

Back | FazBrowse Home | New Git URL