CSV with comments
John Machin
sjmachin at lexicon.net
Tue Jul 18 18:17:53 EDT 2006
On 19/07/2006 5:34 AM, skip at pobox.com wrote:
> >> In csv.reader, is there any way of skip lines that start whith '#' or
> >> empty lines
>
> Nope. When we wrote the module we weren't aware of any "spec" that
> specified comments or blank lines. You can easily write a file wrapper to
> filter them out though:
>
> class BlankCommentCSVFile:
> def __init__(self, fp):
> self.fp = fp
>
> def __iter__(self):
> return self
>
> def next(self):
> line = self.fp.next()
> if not line.strip() or line[0] == "#":
> return self.next()
This is recursive. Unlikely of course, but if the file contained a large
number of empty lines, might this not cause the recursion limit to be
exceeded?
> return line
>
> Use it like so:
>
> reader = csv.reader(BlankCommentCSVFile(open("somefile.csv")))
> for row in reader:
> print row
>
Hi Skip,
Is there any reason to prefer this approach to Daniel's, apart from
being stuck with an older (pre-yield) version of Python?
A file given to csv.reader is supposed to be opened with "rb" so that
newlines embedded in data fields can be handled properly, and also
(according to a post by Andrew MacNamara (IIRC)) for DIY emulation of
"rU". It is not apparent how well this all hangs together when a filter
is interposed, nor whether there are any special rules about what the
filter must/mustn't do. Perhaps a few lines for the docs?
Cheers,
John
More information about the Python-list
mailing list