Reading every 100 lines (or less) from an open file in Python -
Reading every 100 lines (or less) from an open file in Python -
i have file 100s of thousands of records, 1 per line. need read 100, process them, read 100, process them , forth. don't want load many records , maintain them in memory. how read (until eof) either 100 or less (when eof encountered) lines open file using python?
islice() can used retrieve next n items of iterator.
from itertools import islice open(...) file: while true: lines = list(islice(file, 100)) line in lines: # stuff if not lines: break python
Comments
Post a Comment