Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: skip comment lines marked with # in csv.DictReader

Processing CSV files with csv.DictReader is great - but I have CSV files with comment lines (indicated by a hash at the start of a line), for example:

# step size=1.61853 val0,val1,val2,hybridisation,temp,smattr 0.206895,0.797923,0.202077,0.631199,0.368801,0.311052,0.688948,0.597237,0.402763 -169.32,1,1.61853,2.04069e-92,1,0.000906546,0.999093,0.241356,0.758644,0.202382 # adaptation finished 

The csv module doesn't include any way to skip such lines.

I could easily do something hacky, but I imagine there's a nice way to wrap a csv.DictReader around some other iterator object, which preprocesses to discard the lines.

like image 861
Dan Stowell Avatar asked Jan 04 '13 14:01

Dan Stowell


People also ask

How to read a file and Skip initial comment lines in Python?

A naive way to read a file and skip initial comment lines is to use “if” statement and check if each line starts with the comment character “#”. Python string has a nice method “startswith” to check if a string, in this case a line, starts with specific characters. For example, “#comment”.startswith (“#”) will return TRUE.

How do I skip a line in a string in Python?

A naive way to read a file and skip initial comment lines is to use “if” statement and check if each line starts with the comment character “#”. Python string has a nice method “startswith” to check if a string, in this case a line, starts with specific characters.

How to add a multi-line comment in Python?

Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a # for each line:

How do I skip the first few lines of a text file?

The initial few lines of the text file that you want to skip are typically comment or some meta data and starts with some special characters like “#”. Here are 3 ways to read a text file line by line Python and skip initial comment lines. You don’t have to know how many lines you want to skip.


1 Answers

Actually this works nicely with filter:

import csv fp = open('samples.csv') rdr = csv.DictReader(filter(lambda row: row[0]!='#', fp)) for row in rdr:     print(row) fp.close() 
like image 178
Dan Stowell Avatar answered Sep 29 '22 23:09

Dan Stowell