1def rows(f, chunksize=1024, sep='|'):
2 """
3 Read a file where the row separator is '|' lazily.
4
5 Usage:
6
7 >>> with open('big.csv') as f:
8 >>> for r in rows(f):
9 >>> process(r)
10 """
11 row = ''
12 while (chunk := f.read(chunksize)) != '': # End of file
13 while (i := chunk.find(sep)) != -1: # No separator found
14 yield row + chunk[:i]
15 chunk = chunk[i+1:]
16 row = ''
17 row += chunk
18 yield row
19