Python3:使用input()如何从管道(stdin)读取多行? [英] Python3 : Using input() how to Read Multiple Lines from pipe (stdin)?

查看:456
本文介绍了Python3:使用input()如何从管道(stdin)读取多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习python3时我只是好奇,并且在网上没有找到任何好的解释,我的问题也没有.

关于input()的阅读说它是从stdin读取",所以我想我可能会尝试并尝试使用它从管道中读取.确实如此!但只有一条线(直到停产).所以出现的下一个问题是

如何使用input()从管道(stdin)读取多行?

我发现sys.stdin并使用sys.stdin.isatty()来确定stdin是否绑定到tty,假设如果未绑定到tty,则数据来自管道.所以我也找到并成功使用了sys.stdin.readlines()来读取多行.

但仅出于我的好奇心,有没有一种方法可以通过使用普通的input()函数来实现相同的目的?到目前为止,如果stdin包含更多行而不阻塞我的程序,我还没有发现可以测试"的东西.

对不起,如果这一切对您都没有意义.

这是到目前为止我没有输入()的实验代码:

import sys

if sys.stdin.isatty(): # is this a keyboard?
    print(  "\n\nSorry! i only take input from pipe. " 
            "not from a keyboard or tty!\n"
            "for example:\n$ echo 'hello world' | python3 stdin.py"
            ""
            ""
            )
else:
    print ( "reading from stdin via pipe : \n ")

    for line in sys.stdin.readlines():
        print(line, end="")
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can these two lines be replaced with 
#   some construction using plain old input() ? 

解决方案

如果您只想使用input()来访问stdin的行:

print(input()) #prints line 1
print(input()) #prints next line

但是可以说您只想访问第二行:

input() #accesses the first line
print(input()) #prints second line

让我们说您想采用第二行并创建一个数组: 标准输入:

10

64630 11735 14216 99233 14470 4978 73429 38120 51135 67060

input()
values = list(map(int, input().split(' ')))

值将等于[64630,11735,14216,99233,14470,4978,73429,38120,51135,67060]

im just curious while learning python3 and didn't found any good explanation on the web, neither here to my question.

reading about input() it says "reads from stdin" so i thought i might experiment and try to use it to read from pipe. and so it does! but only ONE LINE (till EOL). So the next question that came up was

how to read multiple lines from pipe (stdin) using input() ?

i found sys.stdin and used sys.stdin.isatty() to determine if stdin is bound to a tty or not, assuming that if not bound to tty the data is coming from pipe. and so i also found and used successfully sys.stdin.readlines() too to read multiple lines.

but just for my curiosity , is there a way to achieve the same by using the plain input() function ?? so far i didn't found something "to test" if stdin contains more lines without blocking my program.

sorry if all this makes no sense to you.

this is my experimenting code so far without input():

import sys

if sys.stdin.isatty(): # is this a keyboard?
    print(  "\n\nSorry! i only take input from pipe. " 
            "not from a keyboard or tty!\n"
            "for example:\n$ echo 'hello world' | python3 stdin.py"
            ""
            ""
            )
else:
    print ( "reading from stdin via pipe : \n ")

    for line in sys.stdin.readlines():
        print(line, end="")
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can these two lines be replaced with 
#   some construction using plain old input() ? 

解决方案

if you just want to use input() to access lines of the stdin:

print(input()) #prints line 1
print(input()) #prints next line

but lets say you only wanted to access the second line:

input() #accesses the first line
print(input()) #prints second line

Lets say you wanted to take the second line and create an array: stdin:

10

64630 11735 14216 99233 14470 4978 73429 38120 51135 67060

input()
values = list(map(int, input().split(' ')))

values will equal [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060]

这篇关于Python3:使用input()如何从管道(stdin)读取多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆