csv.Sniffer: wrong detection of the end of line delimiter

Steve Holden steve at holdenweb.com
Thu Dec 29 03:58:34 CET 2005


Laurent Laporte wrote:
> hello,
> 
> I'm using cvs standard module under Python 2.3 / 2.4 to read a CSV
> file. The file is opened in binary mode, so I keep the end of line
> terminator.
> 
It's not advisable to open a file like a CSV, intended for use as text, 
in binary mode.

> It appears that the csv.Sniffer force the line terminator to be
> '\r\n'. It's fine under Windows but wrong under Linux or
> Macintosh.
> 
Perhaps you should try opening the file in text mode, as this will 
normally end up giving you a "\n" terminator on all platforms: that's 
what text mode is intended to ensure, and that's probably why the csv 
module assumes that splitting on "\n" is safe.

> More about this line terminator: Potential bug in the
> _guess_delimiter() method.
> The first line of code does a wrong splitting:
> data = filter(None, data.split('\n'))
> It doesn't take care of the real line terminator!
 > [...]

I suspect it's not supposed to be trying to!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list