Author skip.montanaro
Date 2007-02-11.18:59:09
SpamBayes Score
Marked as misclassified
Sorry, I'm coming back to this after a long hiatus...  I'm still not
inclined to make this change to the C source.  I think a) comments in CSV
files are pretty rare and that b) implementing this using a file object
wrapper would be more flexible.

#!/usr/bin/env python

import csv
import StringIO

class CommentedFile:
    def __init__(self, f, commentstring="#"):
        self.f = f
        self.commentstring = commentstring

    def next(self):
        line =
        while line.startswith(self.commentstring):
            line =
        return line

    def __iter__(self):
        return self

f = StringIO.StringIO('''\
# Rachel Sage
"","Rachael Sage","2008-01-03","2008-01-03","8:00pm"
# Others
"","Bossa Nova Beatniks","2007-11-11","2007-11-11","11:11pm"
"","Special Consensus","2006-10-06","2006-10-06",""

for row in csv.reader(CommentedFile(f)):
    print row

The output of the above is as expected:

['event', 'performers', 'start', 'end', 'time']
['', 'Rachael Sage', '2008-01-03', '2008-01-03', '8:00pm']
['', 'Tung-N-GRoeVE', '2008-01-16', '2008-01-16', '9:30pm-2:00am']
['', 'Bossa Nova Beatniks', '2007-11-11', '2007-11-11', '11:11pm']
['', 'Special Consensus', '2006-10-06', '2006-10-06', '']

This has the added benefit that comment lines aren't restricted to single
character comment prefixes.  On the downside, comment characters appearing
at the beginning of a continuation line would trip this up.  In practice, I
don't think it would be a significant limitation.  In almost all cases I
suspect CSV files with embedded comments would be manually created and
maintained and aren't likely to contain fields with embedded comments.

Date User Action Args
2007-08-23 15:43:21adminlinkissue1225769 messages
2007-08-23 15:43:21admincreate