Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to skip first line when using python fileinput module?

Tags:

python

file-io

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:

import fileinput

# how to skip first line elegantly?

for line in fileinput.input(["file.dat"]):
    data = proces_line(line);
    output(data)
like image 828
stefanB Avatar asked Oct 06 '09 02:10

stefanB


People also ask

How do you skip to a specific line in Python?

There are many ways in which you can skip a line in python. Some methods are: if, continue, break, pass, readlines(), and slicing.

How does Fileinput work Python?

This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see open() . This iterates over the lines of all files listed in sys.


3 Answers

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.

like image 99
nosklo Avatar answered Oct 20 '22 15:10

nosklo


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

like image 32
Phil Avatar answered Oct 20 '22 15:10

Phil


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

like image 34
Jonathan Feinberg Avatar answered Oct 20 '22 15:10

Jonathan Feinberg