簡體   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