Skip to content Skip to sidebar Skip to footer

Skipping Lines, Csv.DictReader

I have a file that has an obnoxious preface to the header. So it looks like this: Review performed by: Meeting: Person: Number: Code: Confirmation Ta

Solution 1:

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

Solution 2:

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 the 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).

iterator = itertools.islice(CSVFile, 14, None)  # Skip header lines.
for row in csv.DictReader(CSVFile, next(iterator).split(), delimiter='\t'):
    # process row ...

Post a Comment for "Skipping Lines, Csv.DictReader"