remove header line when reading/writing files

Tim Chase python.list at tim.thechases.com
Thu Oct 11 19:20:02 EDT 2007


> each file into one master data file.  The code below seems to be doing
> this perfectly.  The problem is each of the data files has a header
> row in the first line, which I do not want in the master file.  How
> can I skip that first line when writing to the master file?  Any help
> is much appreciated.  Thank you.
[snip]
> 		for zipfile in filelist:
> 			filein = gzip.GzipFile(zipfile,'r')
> 			filecontent = filein.read()
> 			filein.close()
> 			outfile.write(filecontent)

for zipfile in filelist:
	for i, line in gzip.Gzipfile(zipfile,'r'):
		if i: outfile.write(line)

should do the trick for you.

If you like a little more readable code, you can change that line to

  if i <> 0: outfile.write(line)

or

  if i == 0: continue
  outfile.write(line)

whichever you like.

-tkc






More information about the Python-list mailing list