For a text I / O stream (for example, from open()or StringIO()), how do I create another stream that filters out strings matching a specific pattern without first reading the entire input stream? I know that I can easily get iterability with help (line for line in input if filter(line)), but I would like a search stream. I also understand that searching will require reading the entire stream to this point, even if the underlying stream allows random access, but this is still better than reading the entire file, as in StringIO("".join(line for line in input if filter(line))).
(As a complement, pointers on how to memoize repeated searches might be welcome!)
source
share