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

时间:2014-07-17 06:42:09

标签: python stdin

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

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

您可以使用enumerate功能来实现:

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

enumerate的文档。

相关问题