我正嘗試使用「from itertools import islice」以便從使用liblas模塊的* .las文件中一次讀取多行代碼。 (我的目標是閱讀的塊狀bychunk)Python:用islice一次讀取N個行數的問題
以下問題:Python how to read N number of lines at a time
islice() can be used to get the next n items of an iterator. Thus, list(islice(f, n)) will return a list of the next n lines of the file f. Using this inside a loop will give you the file in chunks of n lines. At the end of the file, the list might be shorter, and finally the call will return an empty list.
我用下面的代碼:
from numpy import nonzero
from liblas import file as lasfile
from itertools import islice
chunkSize = 1000000
f = lasfile.File(inFile,None,'r') # open LAS
while True:
chunk = list(islice(f,chunkSize))
if not chunk:
break
# do other stuff
,但我有這個問題:
len(f)
2866390
chunk = list(islice(f, 1000000))
len(chunk)
**1000000**
chunk = list(islice(f, 1000000))
len(chunk)
**1000000**
chunk = list(islice(f, 1000000))
len(chunk)
**866390**
chunk = list(islice(f, 1000000))
len(chunk)
**1000000**
當文件f到達時,islice重新開始讀取文件。
感謝您的任何建議和幫助。這是非常欣賞
爾加,那麼你的'lasfile.File'類型是打破所有迭代約定? –
我有一個真正的壞的時刻與lasfile.File –