python读取多行字符串文本

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```
posted @ 2020-10-13 18:38  LiYiming  阅读(2050)  评论(0编辑  收藏  举报