谁能给我一个关于stdin和Python 3中的stdout的快速教程吗?

时间:2021-07-06 00:05:25

I know this sounds like something I can google, but the truth is that I don't find or do not understand what the very few Python 3 sources explains.

我知道这听起来像是我可以谷歌的东西,但事实是,我没有发现或不理解少数Python 3源解释了什么。

So here are my questions:

下面是我的问题:

  • Is input() the stdin function in Python 3? Does that mean that when you open your filename.py program, the stdin is what the user types?
  • 输入()是Python 3中的stdin函数吗?这是否意味着当你打开文件名时。py程序,stdin是什么用户类型?
  • Is print() the stdout function in Python 3, or do you have to write to a file?
  • print()是Python 3中的stdout函数,还是必须写入文件?
  • For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?
  • 对于Spotify的难题,is说“输入是从stdin中读取的”。我的文件应该包括什么stdin和stdout?

Update: Does that mean that i can use:

更新:这是否意味着我可以使用:

import sys
unfmtdDate = str(sys.stdin.read())

...instead of...

…而不是……

unfmtdDate = str(input())

?

吗?

5 个解决方案

#1


7  

stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.

stdin和stdout是操作系统提供的类文件对象。一般来说,当一个程序在交互式会话中运行时,stdin是键盘输入,而stdout是用户的tty,但是shell可以用来将它们从普通文件中重定向到其他程序中,或者将其输出到其他程序中。

input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.

input()用于提示用户输入类型。对于类似编程难题的情况,通常假定stdin是从数据文件重定向的,当输入格式被给定时,最好使用sys.stdin.read()而不是使用input()提示输入。input()用于交互式用户输入,它可以显示一个提示符(在sys.stdout上),并使用GNU readline库(如果存在)来允许行编辑等等。

print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.

print()确实是stdout最常用的写作方式。不需要做任何特殊的事情来指定输出流。print()写入系统。如果没有备用文件作为file= parameter提供给它,则为stdout。

#2


1  

Is input() the stdin function in Python 3? Does that mean that when you open your filename.py program, the stdin is what the user types?

输入()是Python 3中的stdin函数吗?这是否意味着当你打开文件名时。py程序,stdin是什么用户类型?

input() reads from the standard input (called stdin for short in some contexts), yes. No language that I know of has a function named stdin in the standard library. When you run the program in the usual way, what the user types is supplied to standard input, yes. That's what "standard input" means. There is a separate program that is feeding what the user types to standard input (and doing a couple of other convenient things like interpreting the backspace key). Your script is not what creates the window that text is typed into.

input()读取标准输入(在某些上下文中简称为stdin),是的。我所知道的语言中没有一个函数在标准库中有一个名为stdin的函数。当您以通常的方式运行程序时,将哪些用户类型提供给标准输入,是的。这就是“标准输入”的意思。有一个单独的程序向标准输入提供用户输入的内容(并做一些其他方便的事情,比如解释backspace键)。脚本不是创建文本输入的窗口的东西。

sys.stdin (i.e., the value stdin defined in the sys module) is an object that represents the standard input. This is provided so that you can use it in contexts where you're expected to specify "a file", to cause input to be read from the user instead of a file.

sys。stdin(即。,在sys模块中定义的值stdin)是表示标准输入的对象。这是为了让您可以在需要指定“文件”的上下文中使用它,以便从用户而不是文件中读取输入。

Is print() the stdout function in Python 3, or do you have to write to a file?

print()是Python 3中的stdout函数,还是必须写入文件?

print() writes (by default; you can supply a file keyword argument to change this) to the standard output (called stdout for short in some contexts), yes. All the above caveats apply: stdout isn't a function in any language I've ever heard of, and a completely separate program is actually causing the output of your script to be displayed on screen in a pretty 80x24 white-text-on-black-background (or however you have it configured) box.

print()写(默认情况下;您可以为标准输出(在某些上下文中简称为stdout)提供一个文件关键字参数,yes。所有上述注意事项都适用:stdout不是我听说过的任何语言的函数,而且一个完全独立的程序实际上正在使您的脚本的输出显示在一个漂亮的80x24的黑白背景(或无论您如何配置它)框中。

sys.stdout is an object that represents the standard output, used similarly to sys.stdin. You can use it explicitly with the print function, but there's no point: it's the default.

sys。stdout是一个表示标准输出的对象,类似于sys.stdin。您可以使用print函数显式地使用它,但是没有一点:它是默认的。

For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?

对于Spotify的难题,is说“输入是从stdin中读取的”。我的文件应该包括什么stdin和stdout?

The problem specification means "use the input() function to receive input".

问题规范意味着“使用input()函数来接收输入”。

#3


1  

When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).

当你运行你的Python程序时,sys。stdin是连接到标准输入(stdin) sys的文件对象。stdout是标准输出(stdout)和sys的文件对象。stderr是标准错误(stderr)的文件对象。

Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().

在文档的任何地方,您都可以看到对标准输入、标准输出或标准错误的引用,它指的是这些文件句柄。您可以直接访问它们(sys.stdout.write(…)、sys.stdin.read()等)或使用使用这些流的方便函数,如input()和print()。

For the Spotify puzzle, the easiest way to read the input would be something like this:

对于Spotify这个难题,阅读输入信息最简单的方法是:

import sys
data = sys.stdin.read()

After these two lines the input for your program is now in the str data.

在这两行之后,程序的输入现在在str数据中。

#4


0  

From the Python documentation for print:

从Python文档中打印:

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.

文件参数必须是具有写(字符串)方法的对象;如果它不存在或没有,sys。将使用标准输出。输出缓冲是由文件决定的。使用file.flush()确保在屏幕上立即出现。

#5


0  

Is input() the stdin function in Python 3?

input()是Python 3中的stdin函数吗?

Yes.

是的。

Does that mean that when you open your filename.py program, the stdin is what the user types?

这是否意味着当你打开文件名时。py程序,stdin是什么用户类型?

Yes.

是的。

Is print() the stdout function in Python 3, or do you have to write to a file?

print()是Python 3中的stdout函数,还是必须写入文件?

You can use either.

您可以使用。

For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?

对于Spotify的难题,is说“输入是从stdin中读取的”。我的文件应该包括什么stdin和stdout?

Just use import sys to get to the sys.stdin and sys.stdout files. You don't need the import if you use the input and print built-in functions instead.

用import sys来获取sys即可。stdin和系统。标准输出文件。如果使用输入并打印内置函数,则不需要导入。

#1


7  

stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.

stdin和stdout是操作系统提供的类文件对象。一般来说,当一个程序在交互式会话中运行时,stdin是键盘输入,而stdout是用户的tty,但是shell可以用来将它们从普通文件中重定向到其他程序中,或者将其输出到其他程序中。

input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.

input()用于提示用户输入类型。对于类似编程难题的情况,通常假定stdin是从数据文件重定向的,当输入格式被给定时,最好使用sys.stdin.read()而不是使用input()提示输入。input()用于交互式用户输入,它可以显示一个提示符(在sys.stdout上),并使用GNU readline库(如果存在)来允许行编辑等等。

print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.

print()确实是stdout最常用的写作方式。不需要做任何特殊的事情来指定输出流。print()写入系统。如果没有备用文件作为file= parameter提供给它,则为stdout。

#2


1  

Is input() the stdin function in Python 3? Does that mean that when you open your filename.py program, the stdin is what the user types?

输入()是Python 3中的stdin函数吗?这是否意味着当你打开文件名时。py程序,stdin是什么用户类型?

input() reads from the standard input (called stdin for short in some contexts), yes. No language that I know of has a function named stdin in the standard library. When you run the program in the usual way, what the user types is supplied to standard input, yes. That's what "standard input" means. There is a separate program that is feeding what the user types to standard input (and doing a couple of other convenient things like interpreting the backspace key). Your script is not what creates the window that text is typed into.

input()读取标准输入(在某些上下文中简称为stdin),是的。我所知道的语言中没有一个函数在标准库中有一个名为stdin的函数。当您以通常的方式运行程序时,将哪些用户类型提供给标准输入,是的。这就是“标准输入”的意思。有一个单独的程序向标准输入提供用户输入的内容(并做一些其他方便的事情,比如解释backspace键)。脚本不是创建文本输入的窗口的东西。

sys.stdin (i.e., the value stdin defined in the sys module) is an object that represents the standard input. This is provided so that you can use it in contexts where you're expected to specify "a file", to cause input to be read from the user instead of a file.

sys。stdin(即。,在sys模块中定义的值stdin)是表示标准输入的对象。这是为了让您可以在需要指定“文件”的上下文中使用它,以便从用户而不是文件中读取输入。

Is print() the stdout function in Python 3, or do you have to write to a file?

print()是Python 3中的stdout函数,还是必须写入文件?

print() writes (by default; you can supply a file keyword argument to change this) to the standard output (called stdout for short in some contexts), yes. All the above caveats apply: stdout isn't a function in any language I've ever heard of, and a completely separate program is actually causing the output of your script to be displayed on screen in a pretty 80x24 white-text-on-black-background (or however you have it configured) box.

print()写(默认情况下;您可以为标准输出(在某些上下文中简称为stdout)提供一个文件关键字参数,yes。所有上述注意事项都适用:stdout不是我听说过的任何语言的函数,而且一个完全独立的程序实际上正在使您的脚本的输出显示在一个漂亮的80x24的黑白背景(或无论您如何配置它)框中。

sys.stdout is an object that represents the standard output, used similarly to sys.stdin. You can use it explicitly with the print function, but there's no point: it's the default.

sys。stdout是一个表示标准输出的对象,类似于sys.stdin。您可以使用print函数显式地使用它,但是没有一点:它是默认的。

For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?

对于Spotify的难题,is说“输入是从stdin中读取的”。我的文件应该包括什么stdin和stdout?

The problem specification means "use the input() function to receive input".

问题规范意味着“使用input()函数来接收输入”。

#3


1  

When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).

当你运行你的Python程序时,sys。stdin是连接到标准输入(stdin) sys的文件对象。stdout是标准输出(stdout)和sys的文件对象。stderr是标准错误(stderr)的文件对象。

Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().

在文档的任何地方,您都可以看到对标准输入、标准输出或标准错误的引用,它指的是这些文件句柄。您可以直接访问它们(sys.stdout.write(…)、sys.stdin.read()等)或使用使用这些流的方便函数,如input()和print()。

For the Spotify puzzle, the easiest way to read the input would be something like this:

对于Spotify这个难题,阅读输入信息最简单的方法是:

import sys
data = sys.stdin.read()

After these two lines the input for your program is now in the str data.

在这两行之后,程序的输入现在在str数据中。

#4


0  

From the Python documentation for print:

从Python文档中打印:

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.

文件参数必须是具有写(字符串)方法的对象;如果它不存在或没有,sys。将使用标准输出。输出缓冲是由文件决定的。使用file.flush()确保在屏幕上立即出现。

#5


0  

Is input() the stdin function in Python 3?

input()是Python 3中的stdin函数吗?

Yes.

是的。

Does that mean that when you open your filename.py program, the stdin is what the user types?

这是否意味着当你打开文件名时。py程序,stdin是什么用户类型?

Yes.

是的。

Is print() the stdout function in Python 3, or do you have to write to a file?

print()是Python 3中的stdout函数,还是必须写入文件?

You can use either.

您可以使用。

For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?

对于Spotify的难题,is说“输入是从stdin中读取的”。我的文件应该包括什么stdin和stdout?

Just use import sys to get to the sys.stdin and sys.stdout files. You don't need the import if you use the input and print built-in functions instead.

用import sys来获取sys即可。stdin和系统。标准输出文件。如果使用输入并打印内置函数,则不需要导入。