remove header line when reading/writing files

timaranz at gmail.com timaranz at gmail.com
Thu Oct 11 20:28:20 EDT 2007


On Oct 12, 12:23 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> Forgot the enumerate call of all things
>
> > for zipfile in filelist:
> >    for i, line in enumerate(gzip.Gzipfile(zipfile,'r')):
> >            if i: outfile.write(line)
>
> Some days, I'm braindead.
>
> -tkc

I would move the 'if' test outside the loop :

for zipfile in filelist:
    zfiter = iter(gzip.Gzipfile(zipfile,'r'))
    zfiter.next()  # ignore header line
    for i, line in enumerate(fziter):
        outfile.write(line)

I'm not sure if the iter(...) is required.  This will raise a
StopIteration exception if zipfile is empty.

Cheers
Tim




More information about the Python-list mailing list