title: How to filter a text I/O stream in Python
body: <p>Given a text I/O stream (e.g. from <code>open()</code> or <code>StringIO()</code>), how do I create another stream that filters out lines that match a certain pattern, without reading the entire input stream first? I know that I can easily get an iterable with <code>(line for line in input if filter(line))</code>, but I would like a seekable stream. I also understand that seeking would require reading the entire stream up to that point, even if the underlying stream allowed random access, but this is still better than reading the entire file as in <code>StringIO("".join(line for line in input if filter(line)))</code>.</p>
  
  <p>(As an add-on, pointers on how to memoize repeated seeks would be welcome!)</p>