title: How can I use io.StringIO() with the csv module?
body: <p>I tried to backport a Python 3 program to 2.7, and I'm stuck with a strange problem:</p>
  
  <pre><code>&gt;&gt;&gt; import io
  &gt;&gt;&gt; import csv
  &gt;&gt;&gt; output = io.StringIO()
  &gt;&gt;&gt; output.write("Hello!")            # Fail: io.StringIO expects Unicode
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: unicode argument expected, got 'str'
  &gt;&gt;&gt; output.write(u"Hello!")           # This works as expected.
  6L
  &gt;&gt;&gt; writer = csv.writer(output)       # Now let's try this with the csv module:
  &gt;&gt;&gt; csvdata = [u"Hello", u"Goodbye"]  # Look ma, all Unicode! (?)
  &gt;&gt;&gt; writer.writerow(csvdata)          # Sadly, no.
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: unicode argument expected, got 'str'
  </code></pre>
  
  <p>According to the docs, <a href="http://docs.python.org/2.7/library/io.html#io.StringIO"><code>io.StringIO()</code></a> returns an in-memory stream for Unicode text. It works correctly when I try and feed it a Unicode string manually. Why does it fail in conjunction with the <code>csv</code> module, even if all the strings being written are Unicode strings? Where does the <code>str</code> come from that causes the Exception?</p>
  
  <p><sub>(I do know that I can use <code>StringIO.StringIO()</code> instead, but I'm wondering what's wrong with <code>io.StringIO()</code> in this scenario)</sub></p>