简体   繁体   English

如何跳过从标准输入读取的第一行?

[英]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 ?如何跳过从stdin读取的第一行?

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

You can use the enumerate function in order to to that:您可以使用enumerate函数来做到这一点:

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 . enumerate的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM