This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: csv sniffer
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.0, Python 3.1
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: LambertDW, r.david.murray
Priority: normal Keywords: patch

Created on 2009-02-20 20:45 by LambertDW, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
csv.diff skip.montanaro, 2009-02-26 03:32
Messages (3)
msg82546 - (view) Author: David W. Lambert (LambertDW) Date: 2009-02-20 20:45
Following instructions in
I opened file in binary mode.  This might be simply a documentation
problem or it may run deeper.  Text mode works on red hat linux system.

Here's the use:

>>> stream=open('csv','rb')
>>> dialect=csv.Sniffer().sniff(stream.read(1024))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/users/lambert/bin/python/lib/python3.0/csv.py", line 169,
in sniff
    self._guess_quote_and_delimiter(sample, delimiters)
  File "/usr/users/lambert/bin/python/lib/python3.0/csv.py", line 210,
in _guess_quote_and_delimiter
    matches = regexp.findall(data)
TypeError: can't use a string pattern on a bytes-like object

Here's the conflicting documentation statement:

"If csvfile is a file object, it must be opened with the ‘b’ flag on
platforms where that makes a difference. ... see section Dialects and
Formatting Parameters.

All data read are returned as strings. No automatic data type conversion
is performed."

(Problem is that the binary file returnd data of type bytes.)

Thanks, Dave.
msg82740 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2009-02-26 03:32
I verified the bug.  I started to work on a patch (see attached), but
it quickly seems to get out-of-hand with tracebacks about stuff not
supporting the buffer API.  I suspect the real solution might involve
doing something to convert the bytes to strings read when in binary mode,
but I don't have any idea how to finesse the requirement of bytes() for
an encoding arg.
msg106212 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-05-21 01:40
This is in fact a doc bug.  The correct way to read a csv file in python3 is to open it in text mode with newline=''.  The docs have been updated to reflect this.
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49582
2010-05-21 01:40:42r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg106212

resolution: out of date
stage: needs patch -> resolved
2010-05-20 20:27:43skip.montanarosetnosy: - skip.montanaro
2009-02-26 03:32:16skip.montanarosetfiles: + csv.diff
versions: + Python 3.1
nosy: + skip.montanaro
messages: + msg82740
keywords: + patch
stage: needs patch
2009-02-20 20:45:32LambertDWcreate