Python 3中的File對(duì)象不支持next()方法。 Python 3有一個(gè)內(nèi)置函數(shù)next(),它通過(guò)調(diào)用其next ()方法從迭代器中檢索下一個(gè)項(xiàng)目。 如果給定了默認(rèn)值,則在迭代器耗盡返回此默認(rèn)值,否則會(huì)引發(fā)StopIteration。 該方法可用于從文件對(duì)象讀取下一個(gè)輸入行。
語(yǔ)法
以下是next()方法的語(yǔ)法 -
next(iterator[,default])
參數(shù)
返回值
此方法返回下一個(gè)輸入行
英文文檔:
next(iterator[, default])
Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
說(shuō)明:
1. 函數(shù)必須接收一個(gè)可迭代對(duì)象參數(shù),每次調(diào)用的時(shí)候,返回可迭代對(duì)象的下一個(gè)元素。如果所有元素均已經(jīng)返回過(guò),則拋出StopIteration 異常。
>>> a = iter('abcd')>>> next(a)'a'>>> next(a)'b'>>> next(a)'c'>>> next(a)'d'>>> next(a)Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> next(a)StopIteration2. 函數(shù)可以接收一個(gè)可選的default參數(shù),傳入default參數(shù)后,如果可迭代對(duì)象還有元素沒(méi)有返回,則依次返回其元素值,如果所有元素已經(jīng)返回,則返回default指定的默認(rèn)值而不拋出StopIteration 異常。
>>> a = iter('abcd')>>> next(a,'e')'a'>>> next(a,'e')'b'>>> next(a,'e')'c'>>> next(a,'e')'d'>>> next(a,'e')'e'>>> next(a,'e')'e'以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選