CSV with comments
Daniel Dittmar
daniel.dittmar at sap.corp
Tue Jul 18 12:03:42 EDT 2006
GinTon wrote:
> GinTon wrote:
>> In csv.reader, is there any way of skip lines that start whith '#' or
>> empty lines
>> I would add comments at my CSV file
>
> For skip comment I get a dirty trick:
>
> reader = csv.reader(open(csv_file))
> for csv_line in reader:
> if csv_line[0].startswith('#'):
> continue
>
> But not possible let blank lines.
>
> I think that CSV class should to let skip comments and new lines of
> auto. way.
>
write an iterator that filters line to your liking and use it as input
to cvs.reader:
def CommentStripper (iterator):
for line in iterator:
if line [:1] == '#':
continue
if not line.strip ():
continue
yield line
reader = csv.reader (CommentStripper (open (csv_file)))
CommentStripper is actually quite useful for other files. Of course
there might be differences if a comment starts
- on the first character
- on the first non-blank character
- anywhere in the line
Daniel
More information about the Python-list
mailing list