You could wrap the CSVFile
with an itertools.islice
iterator object to slice-off the lines of the preface when creating the DictReader
, instead of providing it directly to the constructor.
This works because the csv.reader
constructor will accept "any object which supports the iterator protocol and returns a string each time its __next__()
method is called" as its first argument according to the csv docs. This also applies to csv.DictReader
s because they're implemented via an underlying csv.reader
instance.
Note how the next(iterator).split()
expression supplies the csv.DictReader
with a fieldnames
argument (so it's not taken it from the first line of the file when it's instantiated) — so what @jonrsharpe said about the first line being read when the DictReader
is instantiated isn't strictly true.)
iterator = itertools.islice(CSVFile, 14, None) # Skip header lines.for row in csv.DictReader(CSVFile, next(iterator).split(), delimiter='\t'): # process row ...