繁体   English   中英

sys.stdin.read() 和 sys.stdin.readline() 有什么区别

[英]What is the difference between sys.stdin.read() and sys.stdin.readline()

具体来说,我想知道在 read() 的情况下如何提供输入。 我到处尝试,但在任何地方都找不到差异。

>>> help(sys.stdin.read)
Help on built-in function read:

read(size=-1, /) method of _io.TextIOWrapper instance
    Read at most n characters from stream.
    
    Read from underlying buffer until we have n characters or we hit EOF.
    If n is negative or omitted, read until EOF.
(END)

所以你需要在完成后发送 EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return):

>>> sys.stdin.read()
asd
123
'asd\n123\n'

readline线很明显。 它将一直读取到换行符或 EOF。 因此,您可以在完成后按 Enter 键。

read()识别每个字符并打印它。

readline()逐行识别 object 并将其打印出来。

暂无
暂无

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

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