谁能给我一个关于 Python 3 中标准输入和标准输出的快速教程? [英] Can anyone give me a quick tutorial in stdin and stdout in Python 3?

查看:23
本文介绍了谁能给我一个关于 Python 3 中标准输入和标准输出的快速教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来像我可以谷歌搜索的东西,但事实是我没有找到或不理解极少数 Python 3 来源解释的内容.

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.

这里是我的问题:

  • input() 是 Python 3 中的 stdin 函数吗?这是否意味着当您打开 filename.py 程序时,stdin 就是用户键入的内容?
  • print() 是 Python 3 中的 stdout 函数,还是必须写入文件?
  • 对于 Spotify 拼图, 表示从标准输入读取输入".我的文件应该包括 stdinstdout 什么?
  • 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?
  • Is print() the stdout function in Python 3, or do you have to write to a file?
  • For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?

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

Update: Does that mean that i can use:

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

...而不是...

unfmtdDate = str(input())

?

推荐答案

stdinstdout 是操作系统提供的类文件对象.通常,当程序在交互式会话中运行时,stdin 是键盘输入,stdout 是用户的 tty,但 shell 可用于将它们从普通文件重定向或通过管道传输其他程序的输出和输入.

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.

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

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.

print() 实际上是写入 stdout 的最常用方式.没有必要做任何特殊的事情来指定输出流.print() 如果没有将替代文件作为 file= 参数提供给 sys.stdout,则写入 sys.stdout.

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.

这篇关于谁能给我一个关于 Python 3 中标准输入和标准输出的快速教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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