Özlem
Özlem

Reputation: 207

How can I skip first line reading from stdin?

 while 1:
     try:
         #read from stdin
         line = sys.stdin.readline()
     except KeyboardInterrupt:
         break
     if not line:
         break
     fields = line.split('#')
     ...

How can I skip first line reading from stdin?

Upvotes: 9

Views: 10374

Answers (2)

John La Rooy
John La Rooy

Reputation: 304195

infile = sys.stdin
next(infile) # skip first line of input file
for line in infile:
     if not line:
         break
     fields = line.split('#')
     ...

Upvotes: 9

ro.e
ro.e

Reputation: 319

You can use the enumerate function in order to to that:

for place, line in enumerate(sys.stdin):
    if place: # when place == 0 the if condition is not satisfied (skip first line) 
        ....

The documentation of enumerate.

Upvotes: 3

Related Questions