title: How to modify lines in Python stream
body: <p>I am using psycopg2 to export a PostgreSQL table.</p>
  <pre class="lang-py prettyprint-override"><code>file = sys.stdout # writable file
  cur.copy_to(file, &quot;example_table&quot;)
  </code></pre>
  <p>I have a transformation that I would like to apply to each line of the file as it is exported.</p>
  <pre class="lang-py prettyprint-override"><code>def transform(line):
    # ...
    return line
  </code></pre>
  <p>How should I do this?</p>
  <p>I could implement my own TextIO class from scratch, but it seems like there ought to be a simple, less error-prone way to modify this output line by line.</p>