Unexpected behaviour of csv module

John Machin sjmachin at lexicon.net
Mon Sep 25 05:00:38 EDT 2006


Andrew McLean wrote:
> John Machin wrote:
> > A better workaround IMHO is to strip each *field* after it is received
> > from the csv reader. In fact, it is very rare that leading or trailing
> > space in CSV fields is of any significance at all. Multiple spaces
> > ditto. Just do this all the time:
> >
> > row = [' '.join(x.split()) for x in row]
>
> The problem with removing the spaces after they are received from the
> csv reader is if you want to use DictReader. I like to use DictReader,
> without passing it the field list. The module then reads the field list
> from the first line, and in this situation you don't get an opportunity
> to strip the spaces from that.

You can fix that. The beauty of open source is that you can grab it
(Windows: c:\python2?\lib\csv.py (typically)) and hack it about till it
suits your needs. Go fer it!

Cheers,
John




More information about the Python-list mailing list