Question

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

Was it helpful?

Solution

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

for linenum, line in enumerate(sys.stdin):
    if linenum != 0:
        process line
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top