Pythonic use of CSV module to skip headers?

Steve Holden steve at holdenweb.com
Thu Dec 2 18:24:58 EST 2004


Ramon Felciano wrote:

> Hi --
> 
> I'm using the csv module to parse a tab-delimited file and wondered
> whether there was a more elegant way to skip an possible header line.
> I'm doing
> 
> 	line = 0
> 	reader = csv.reader(file(filename))
> 	for row in reader:
> 		if (ignoreFirstLine & line == 0):
> 			continue
> 		line = line+1
> 		# do something with row
> 
> The only thing I could think of was to specialize the default reader
> class with an extra skipHeaderLine constructor parameter so that its
> next() method can skip the first line appropriate. Is there any other
> cleaner way to do it w/out subclassing the stdlib?
> 
> Thanks!
> 
> Ramon

How about

	line = 0
	reader = csv.reader(file(filename))
	headerline = reader.next()
	for row in reader:
		line = line+1
		# do something with row

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list