How to read line by line from stdin in python

2019-04-18 17:13发布

问题:

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)

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.

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

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!

回答1:

It's simpler:

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

The file-like object sys.stdin is 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 that character-by.character.



回答2:

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)))


回答3:

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

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 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.

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.



标签: python stdin