简体   繁体   English

python-sys.stdin.readlines() ,停止在命令行中读取行

[英]python-sys.stdin.readlines() ,Stop reading lines in command line

I have a code with function sys.stdin.readlines() .我有一个函数sys.stdin.readlines()的代码。

  1. What is the difference between the above one and sys.stdin.buffer.readlines() ?.上面sys.stdin.buffer.readlines()sys.stdin.buffer.readlines()什么sys.stdin.buffer.readlines() ?。

  2. What exactly do they do ?他们究竟是做什么的?

  3. If they read lines from command line,how to stop reading lines at a certain instant and proceed to flow through the program?如果他们从命令行读取行,如何在某个时刻停止读取行并继续流经程序?

1) sys.stdin is a TextIOWrapper , its purpose is to read text from stdin. 1) sys.stdin是一个TextIOWrapper ,它的目的是从 stdin 读取文本。 The resulting strings will be actual str s.结果字符串将是实际的str s。 sys.stdin.buffer is a BufferedReader . sys.stdin.buffer是一个BufferedReader The lines you get from this will be byte strings您从中获得的行将是字节字符串

2) They read all the lines from stdin until hitting eof or they hit the limit you give them 2)他们从stdin读取所有行,直到达到eof或者达到你给他们的限制

3) If you're trying to read a single line, you can use .readline() (note: no s ). 3)如果你想读一行,你可以使用.readline() (注意:没有s )。 Otherwise, when interacting with the program on the command line, you'd have to give it the EOF signal (Ctrl+D on *nix)否则,当在命令行上与程序交互时,你必须给它 EOF 信号(Ctrl+D on *nix)

Is there a reason you are doing this rather than just calling input() to get one text line at a time from stdin?您这样做是否有原因而不是仅仅调用input()从标准输入一次获取一个文本行?

From the docs文档

sys.stdin系统标准输入

sys.stdout系统标准输出

sys.stderr系统文件

File objects corresponding to the interpreter's standard input, output and error streams.与解释器的标准输入、输出和错误流相对应的文件对象。 stdin is used for all interpreter input except for scripts but including calls to input(). stdin 用于除脚本外的所有解释器输入,但包括对 input() 的调用。 stdout is used for the output of print() and expression statements and for the prompts of input(). stdout 用于 print() 和表达式语句的输出以及 input() 的提示。 The interpreter's own prompts and (almost all of) its error messages go to stderr.解释器自己的提示和(几乎所有)它的错误消息都转到 stderr。 stdout and stderr needn't be built-in file objects: any object is acceptable as long as it has a write() method that takes a string argument. stdout 和 stderr 不需要是内置文件对象:任何对象都是可以接受的,只要它有一个带字符串参数的 write() 方法。 (Changing these objects doesn't affect the standard I/O streams of processes executed by os.popen(), os.system() or the exec*() family of functions in the os module.) (更改这些对象不会影响由 os.popen()、os.system() 或 os 模块中的 exec*() 系列函数执行的进程的标准 I/O 流。)

Note: The standard streams are in text mode by default.注意:默认情况下,标准流处于文本模式。 To write or read binary data to these, use the underlying binary buffer.要向这些写入或读取二进制数据,请使用底层二进制缓冲区。 For example, to write bytes to stdout, use sys.stdout.buffer.write(b'abc') .例如,要将字节写入标准输出,请使用sys.stdout.buffer.write(b'abc')

So, sys.stdin.readlines() reads everything that was passed to stdin and separates the contents so lines are formed (you get a list as a result).所以, sys.stdin.readlines()读取传递给所有stdin和分离,从而形成线的内容(你会看到一个列表的结果)。

sys.stdin.buffer.readlines() does the same, but for buffer of stdin. sys.stdin.buffer.readlines()做同样的sys.stdin.buffer.readlines() ,但对于标准输入的缓冲区。 I'd suggest to use the first method as the buffer may be empty while stdin may contain some data.我建议使用第一种方法,因为缓冲区可能是空的,而 stdin 可能包含一些数据。

If you want to stop at some moment, then use readline() to read only one line at a time .如果您想在某个时刻停止,则使用readline()一次只读取一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM