views:

359

answers:

3

Is there an elegant way of skipping first line of file when using python fileinput module?

I have data file with nicely formated data but the first line is header. Using fileinput I would have to include check and discard line if the line does not seem to contain data.

The problem is that it would apply the same check for the rest of the file. With read() you can open file, read first line then go to loop over the rest of the file. Is there similar trick with fileinput?

Is there an elegant way to skip processing of the first line?

Example code:

input fileinput

# how to skip first line elegantly?

for line in fileinput.input(["file.dat"]):
    data = proces_line(line);
    output(data)
+4  A: 

It's right in the docs: http://docs.python.org/library/fileinput.html#fileinput.isfirstline

Jonathan Feinberg
+1 yes it is, and I missed it ... :)
stefanB
+5  A: 

The fileinput module contains a bunch of handy functions, one of which seems to do exactly what you're looking for:

for line in fileinput.input(["file.dat"]):
  if not fileinput.isfirstline():
    data = proces_line(line);
    output(data)

fileinput module documentation

Phil
+1 nice, I'm checking it out right now even though from here it looks like the check is applied for every line ... but I may be wrong, plus it's probably just checking the line number so I don't have to ...
stefanB
+2  A: 
lines = iter(fileinput.input(["file.dat"]))
next(lines) # extract and discard first line
for line in lines:
    data = proces_line(line)
    output(data)

or use the itertools.islice way if you prefer

import itertools
finput = fileinput.input(["file.dat"])
lines = itertools.islice(finput, 1, None) # cuts off first line
dataset = (process_line(line) for line in lines)
results = [output(data for data in dataset]

Since everything used are generators and iterators, no intermediate list will be built.

nosklo