Skipping lines, csv.DictReader

A csv.DictReader reads the first line from the file when it's instantiated, to get the headers for subsequent rows. Therefore it uses Review performed by: as the header row, then you skip the next 14 rows.

Instead, skip the lines before creating the DictReader:

for i in range(14):
    CSVFile.next()
reader = csv.DictReader(CSVFile)
...

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.DictReaders 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 ...

Tags:

Python

Csv