Python 如何跳过标准输入的第一行阅读?

Python 如何跳过标准输入的第一行阅读?,python,stdin,Python,Stdin,如何跳过stdin中的第一行读取?您可以使用枚举功能来: while 1: try: #read from stdin line = sys.stdin.readline() except KeyboardInterrupt: break if not line: break fields = line.split('#') ... 的文档。这比我的示例要好得多:)出于好

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

您可以使用
枚举
功能来:

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

的文档。

这比我的示例要好得多:)出于好奇:为什么不直接执行
next(sys.stdin)
?为什么给它取别名?@exhuma,我觉得它更可读一点。其他人可能喜欢在任何地方使用
sys.stdin
。只是个人喜好(直到你需要重构代码,以便可以选择从其他地方读取为止)如果你想预见重构,我会将其放入一个函数中,以文件为参数;)--但我明白你的意思:)我觉得用
枚举
来做这件事是浪费(只是一种直觉)。看起来不对。有更清洁的解决方案。我发现@gnibbler是迄今为止最具Python风格的解决方案。
for place, line in enumerate(sys.stdin):
    if place: # when place == 0 the if condition is not satisfied (skip first line) 
        ....
infile = sys.stdin
next(infile) # skip first line of input file
for line in infile:
     if not line:
         break
     fields = line.split('#')
     ...