Reading csv file

Peter Otten __peter__ at web.de
Tue Dec 17 03:55:43 EST 2013


Peter Otten wrote:

> You are still reading the complete csv file. Assuming
> 
> (1) the first row of the csv contains the column names
> (2) you want to skip the first five rows of data
> 
> you'd have to write
> 
> reader = csv.Reader(file)

Sorry, I meant DictReader, not Reader.

> line = 0
> while line < 5:
>     next(reader)
>     line += 1
> for row in reader:
>     .... # process csv row
> 
> A simpler alternative is to use itertools.islice():
> 
> for row in itertools.islice(reader, 5, None):
>     ... # process csv row





More information about the Python-list mailing list