Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for Skipping lines, csv.DictReader

$
0
0

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

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>