如何从python中的stdin逐行读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20816375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:17:01  来源:igfitidea点击:

How to read line by line from stdin in python

pythonstdin

提问by Storm-Eyes

Everyone knows how to count the characters from STDIN in C. However, when I tried to do that in python3, I find it is a puzzle. (counter.py)

每个人都知道如何在 C 中计算 STDIN 中的字符数。但是,当我尝试在 python3 中这样做时,我发现这是一个难题。(计数器.py)

import sys
chrCounter = 0

for line in sys.stdin.readline():
    chrCounter += len(line)

print(chrCounter)

Then I try to test the program by

然后我尝试通过

python3 counter.py < counter.py

The answer is only the length of the first line "import sys". In fact, the program ONLY read the first line from the standard input, and discard the rest of them.

答案只是第一行“import sys”的长度。事实上,程序只从标准输入中读取第一行,并丢弃其余的行。

It will be work if I take the place of sys.stdin.readline by sys.stdin.read()

如果我用 sys.stdin.read() 代替 sys.stdin.readline 会起作用

import sys
print(len(sys.stdin.read()))

However, it is obviously, that the program is NOT suitable for a large input. Please give me a elegant solution. Thank you!

但是,很明显,该程序不适合大输入。请给我一个优雅的解决方案。谢谢!

回答by Tim Pietzcker

It's simpler:

更简单:

for line in sys.stdin:
    chrCounter += len(line)

The file-like object sys.stdinis automatically iterated over line by line; if you call .readline()on it, you only read the first line (and iterate over that character-by-character); if you call read(), then you'll read the entire input into a single string and iterate over thatcharacter-by.character.

类文件对象sys.stdin会自动逐行迭代;如果你调用.readline()它,你只会阅读第一行(并逐个字符地迭代);如果你打电话read(),那么你会在读取整个输入一个字符串和迭代字符by.character。

回答by Rob?

If I just wanted a character count, I'd read in blocks at a time instead of lines at a time:

如果我只是想要一个字符数,我会一次读取一个块而不是一次:

# 4096 chosen arbitrarily. Pick any other number you want to use.
print(sum(iter(lambda:len(sys.stdin.read(4096)), 0)))

回答by Posa

The answer from Tim Pietzcker is IMHO the correct one. There are 2 similar ways of doing this. Using:

Tim Pietzcker 的答案恕我直言是正确的。有两种类似的方法可以做到这一点。使用:

for line in sys.stdin:

and

for line in sys.stdin.readlines():

The second option is closer to your original code. The difference between these two options is made clear by using e.g. the following modification of the for-loop body and using keyboard for input:

第二个选项更接近您的原始代码。这两个选项之间的区别可以通过使用例如以下对 for 循环体的修改和使用键盘输入来明确:

for line in sys.stdin.readlines():
    line_len = len(line)
    print('Last line was', line_len, 'chars long.')
    chrCounter += len(line)

If you use the first option (for line in sys.stdin:), then the lines are processed right after you hit enter.

如果您使用第一个选项 ( for line in sys.stdin:),则在您按 Enter 键后会立即处理这些行。

If you use the second option (for line in sys.stdin.readlines():), then the whole file is first read, split into lines and only then they are processed.

如果您使用第二个选项 ( for line in sys.stdin.readlines():),则首先读取整个文件,拆分成行,然后才处理它们。