Python sys.stdin 读什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29454365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:34:25  来源:igfitidea点击:

What does sys.stdin read?

pythonfilestdinsys

提问by Vimzy

I get how to open files, and then use Python's pre built in functions with them. But how does sys.stdin work?

我了解如何打开文件,然后将 Python 的预内置函数与它们一起使用。但是 sys.stdin 是如何工作的呢?

for something in sys.stdin:
    some stuff here

lines = sys.stdin.readlines()

What's the difference between the above two different uses on sys.stdin? Where is it reading the information from? Is it via keyboard, or do we still have to provide a file?

sys.stdin 上面两种不同的用法有什么区别?它从哪里读取信息?是通过键盘,还是我们还需要提供文件?

采纳答案by cdarke

So you have used Python's "pre built in functions", presumably like this:

所以你已经使用了 Python 的“预内置函数”,大概是这样的:

file_object = open('filename')
for something in file_object:
    some stuff here

This reads the file by invoking an iteratoron the file object which happens to return the next line from the file.

这通过调用文件对象上的迭代器来读取文件,该迭代器恰好返回文件的下一行。

You could instead use:

您可以改为使用:

file_object = open('filename')
lines = file_object.readlines()

which reads the lines from the current file position into a list.

它将当前文件位置的行读取到列表中。

Now, sys.stdinis just another file object, which happens to be opened by Python before your program starts. What you do with that file object is up to you, but it is not really any different to any other file object, its just that you don't need an open.

现在,sys.stdin只是另一个文件对象,它恰好在您的程序启动之前被 Python 打开。你对那个文件对象做什么取决于你,但它与任何其他文件对象并没有什么不同,只是你不需要open.

for something in sys.stdin:
    some stuff here

will iterate through standard input until end-of-file is reached. And so will this:

将遍历标准输入,直到到达文件结尾。这也是:

lines = sys.stdin.readlines()

Your first question is really about different ways of using a file object.

您的第一个问题实际上是关于使用文件对象的不同方式。

Second, where is it reading from? It is reading from file descriptor 0 (zero). On Windows it is file handle 0 (zero). File descriptor/handle 0 is connected to the console or tty by default, so in effect it is reading from the keyboard. However it can be redirected, often by a shell (like bash or cmd.exe) using syntax like this:

其次,它从哪里读取?它正在从文件描述符 0(零)读取。在 Windows 上,它是文件句柄 0(零)。默认情况下,文件描述符/句柄 0 连接到控制台或 tty,因此实际上它是从键盘读取的。然而,它可以被重定向,通常由 shell(如 bash 或 cmd.exe)使用如下语法:

myprog.py < input_file.txt 

That alters file descriptor zero to read a file instead of the keyboard. On UNIX or Linux this uses the underlying call dup2(). Read your shell documentation for more information about redirection (or maybe man dup2if you are brave).

这会改变文件描述符零以读取文件而不是键盘。在 UNIX 或 Linux 上,这使用底层调用dup2()。阅读您的 shell 文档以获取有关重定向的更多信息(或者man dup2如果您很勇敢的话)。

回答by Burhan Khalid

It is reading from the standard input- and it should be provided by the keyboard in the form of stream data.

它从标准输入中读取——它应该由键盘以流数据的形式提供。

It is not required to provide a file, however you can use redirectionto use a file as standard input.

不需要提供文件,但是您可以使用重定向将文件用作标准输入。

In Python, the readlines()method reads the entire stream, and then splits it up at the newline characterand creates a list of each line.

在 Python 中,该readlines()方法读取整个流,然后在换行符处将其拆分并创建每一行的列表。

lines = sys.stdin.readlines()

The above creates a list called lines, where each element will be a line (as determined by the end of line character).

上面创建了一个名为 lines 的列表,其中每个元素都是一行(由行尾字符确定)。

You can read more about this at the input and output sectionof the Python tutorial.

您可以在 Python 教程的输入和输出部分阅读有关此内容的更多信息。

If you want to prompt the user for input, use the input()method (in Python 2, use raw_input()):

如果要提示用户输入,请使用该input()方法(在 Python 2 中,使用raw_input()):

user_input = input('Please enter something: ')
print('You entered: {}'.format(user_input))

回答by Anthony Kong

for something in sys.stdin:
    some stuff here

The code above does not work as you expect because sys.stdinis a file handle - it is a file handle to the stdin. It will not reach the some stuff hereline

上面的代码不能像您期望的那样工作,因为它sys.stdin是一个文件句柄 - 它是stdin. 它不会到达some stuff here线

lines = sys.stdin.readlines()

When the script above is run in an interactive shell, it will block the execution until a user presses Ctrl-D, which indicates the end of the input.

当上面的脚本在交互式 shell 中运行时,它将阻止执行,直到用户按下 Ctrl-D,这表明输入结束。

回答by Andrushenko Alexander

To get a grasp how sys.stdin works do following:

要了解 sys.stdin 的工作原理,请执行以下操作:

create a simple python script, let's name it "readStdin.py":

创建一个简单的python脚本,让我们将其命名为“readStdin.py”:

import sys
lines = sys.stdin.readlines()
print (lines)

Now open console any type in:

现在打开控制台任何类型:

echo "line1 line2 line3" | python readStdin.py

The script outputs:

脚本输出:

['"line1 line2 line3" \n']

So, the script has read the input into list (named 'lines'), including the new line character produced by 'echo'. That is.

因此,脚本已将输入读入列表(名为 'lines'),包括由 'echo' 生成的换行符。那是。

回答by Mr. Suryaa Jha

According to me sys.stdin.read() method accepts a line as the input from the user until a special character like Enter Key and followed by Ctrl + D and then stores the input as the string.

根据我的说法, sys.stdin.read() 方法接受一行作为用户的输入,直到像 Enter Key 这样的特殊字符,然后是 Ctrl + D,然后将输入存储为字符串。

Control + D works as the stop signal.

Control + D 用作停止信号。

Example:

例子:

import sys

input = sys.stdin.read()
print(input)
tokens = input.split()
a = int(tokens[0])
b = int(tokens[1])
print(a + b)

After running the program enter two numbers delimited by space and after finishing press Control + D once or twice and you will be presented by the sum of the two inputs.

运行程序后输入两个由空格分隔的数字,完成后按 Control + D 一次或两次,您将看到两个输入的总和。