title: Python: Write Unicode to CSV using UnicodeWriter
body: <p>Python Documents have following code example on writing unicode to csv file. I think it has mentioned there that this is the way to do since csv module can't handle unicode strings.</p>
  
  <pre><code>class UnicodeWriter:
      """
      A CSV writer which will write rows to CSV file "f",
      which is encoded in the given encoding.
      """
  
      def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
          # Redirect output to a queue
          self.queue = cStringIO.StringIO()
          self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
          self.stream = f
          self.encoder = codecs.getincrementalencoder(encoding)()
  
      def writerow(self, row):
          self.writer.writerow([s.encode("utf-8") for s in row])
          # Fetch UTF-8 output from the queue ...
          data = self.queue.getvalue()
          data = data.decode("utf-8")
          # ... and reencode it into the target encoding
          data = self.encoder.encode(data)
          # write to the target stream
          self.stream.write(data)
          # empty queue
          self.queue.truncate(0)
  
      def writerows(self, rows):
          for row in rows:
              self.writerow(row)
  </code></pre>
  
  <p>I am writing more than one file and to keep it simple I have only put the section of my code to demonstrate how I use above class in my code:</p>
  
  <pre><code>def write(self):
      """
      Outputs the dataset to a csv.
      """
      f = codecs.open(self.filename, 'a')
      writer = UnicodeWriter(f)
      #with open(self.filename, 'a', encoding='utf-8') as f:
      if self.headers and not self.written:
          writer.writerow(self.headers)
          self.written = True
      for record in self.records[self.last_written:]:
          print record
          writer.writerow(record)
      self.last_written = len(self.records)
      f.close()
  </code></pre>
  
  <p>This is a method inside a class coll dataset which prepare the dataset prior to writing to csv, previously I was using <code>writer = csv.writer(f)</code> but due to codec errors I change my code to use `UnicodeWriter class. </p>
  
  <p>But my problem is that when I open the csv file, I get the following: </p>
  
  <pre><code>some_header
  B,r,ë,k,ò,w,n,i,k,_,b,s
  B,r,ë,k,ò,w,n,i,k,_,c,s
  B,r,ë,k,ò,w,n,i,k,_,c,s,b
  B,r,ë,k,ò,w,n,i,k,_,d,e
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,1
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,2
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,3
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,4
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,5
  B,r,ë,k,ò,w,n,i,k,_,d,e,-,M
  B,r,ë,k,ò,w,n,i,k,_,e,n
  B,r,ë,k,ò,w,n,i,k,_,e,n,-,1
  B,r,ë,k,ò,w,n,i,k,_,e,n,-,2
  </code></pre>
  
  <p>Where as these rows should actually should be something like <code>Brëkòwnik_de-1</code> I am not really whats happening. </p>
  
  <p>To give a basic idea of how the data has been generated I would add the following line:
  <code>title = unicode(row_page_title['page_title'], 'utf-8')</code></p>