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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM