python - How to stop iterating using itertools.islice when EOF is reached -
i use itertools.islice(self._f, 0, 100, none)
read in file piece piece (in blocks of 100 lines) follows:
f = open('test.dat', 'r') while (some condition for): f = open(filename, 'r') x = itertools.islice(f, 0, 100, none) dosomethingwithx(x)
my problem is, not know how long file , looking condition stop while loop when end of file reached. cannot figure out how done.
edit: ok, see difficulty. maybe should reformulate question when itertools.islice capsuled in class here:
class reader: def __init__() self._f = open('test.dat', 'r') def getnext(): return itertools.islice(self._f, 0, 100, none) r = reader() while (some condition for): x = r.getnext() dosomethingwithx(x)
if don't mind getting list slices, can use iter
:
with open(filename, 'r') f: x in iter(lambda: list(itertools.islice(f, 100)), []): dosomethingwithx(x)
not sure file using have f = ..
twice , have self_.f
in there too.
using edited code:
class reader: def __init__(self): self._f = open('out.csv', 'r') def getnext(self): return itertools.islice(self._f, 100) r = reader() import itertools x in iter(lambda: list(r.getnext()),[]): print(x)
using test file following , class code using itertools.islice(self._f, 2)
:
1 2 3 4 5 6 7 8 9 10
outputs:
in [15]: r = reader() in [16]: import itertools in [17]: x in iter(lambda: list(r.getnext()),[]): ....: print(x) ....: ['1\r\n', '2\r\n'] ['3\r\n', '4\r\n'] ['5\r\n', '6\r\n'] ['7\r\n', '8\r\n'] ['9\r\n', '10']
Comments
Post a Comment