stri = ''
try:
for line in iter(input, stopword):
stri += line + '\n'
except EOFError:
pass
stri = stri[0:-1]
# do something……```
其中,stopword代表空字符船:當讀取到最后一行之后,我們繼續讀取input就是空字符串,此時停止讀取。
下面是python中自帶iter的函數。第二個參數stopword的作用是停止符。
```def iter(source, sentinel=None): # known special case of iter
"""
iter(iterable) -> iterator
iter(callable, sentinel) -> iterator
Get an iterator from an object. In the first form, the argument must
supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
"""
pass```