如何跳过stdin的第一行读数?

问题描述 投票:6回答:3
 while 1:
     try:
         #read from stdin
         line = sys.stdin.readline()
     except KeyboardInterrupt:
         break
     if not line:
         break
     fields = line.split('#')
     ...

如何跳过从stdin中读取的第一行?stdin?

python stdin
3个回答
6
投票
infile = sys.stdin
next(infile) # skip first line of input file
for line in infile:
     if not line:
         break
     fields = line.split('#')
     ...

3
投票

你可以使用 enumerate 函数,以对该。

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

记载: 列举.

© www.soinside.com 2019 - 2024. All rights reserved.