Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip first line with Python stdin?

Tags:

python

io

stdin

I would like to read lines from the python stdin implementation. so far I have:

for line in sys.stdin:
    process line

but I would like to skip the first line that is supplied.

Python has a method for this when using the fileinput implementation which returns true if it is the first line and false otherwise

fileinput.isfirstline()

Ideally there would be something like that for stdin where I could go:

if sys.stdin.isfirstline():
     process(first line)
else:
     process everthing else

Is there a way to do this?

Thanks

like image 659
birthofearth Avatar asked Mar 21 '23 03:03

birthofearth


1 Answers

You can use enumerate to keep track of the line number:

for linenum, line in enumerate(sys.stdin):
    if linenum != 0:
        process line
like image 118
Darrick Herwehe Avatar answered Apr 02 '23 09:04

Darrick Herwehe