Scientific data commonly comes in tab-separated textfile format containing comment lines. What is the best way to read this data? Analogous to the recipe given by skip.montanaro, use a commented file decorator as follows:
import sys, re import csv class CommentedFile: def __init__(self, f, commentstring="#"): self.f = f self.commentstring = commentstring def next(self): line = self.f.next() while line.startswith(self.commentstring): line = self.f.next() return line def __iter__(self): return self tsv_file = csv.reader(CommentedFile(open("inputfile.txt", "rb")), delimiter='\t') for row in tsv_file: print row[2] # prints column 3 of each line