Last active
December 16, 2015 20:39
-
-
Save daleobrien/5493602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import csv | |
import cStringIO | |
import codecs | |
class DictUnicodeWriter(object): | |
def __init__(self, f, fieldnames, dialect=csv.excel, encoding="utf-8", **kwds): | |
# Redirect output to a queue | |
self.queue = cStringIO.StringIO() | |
self.writer = csv.DictWriter(self.queue, fieldnames, dialect=dialect, **kwds) | |
self.stream = f | |
self.encoder = codecs.getincrementalencoder(encoding)() | |
def writerow(self, row): | |
_row = {} | |
for k, v in row.items(): | |
if isinstance(v, str) or isinstance(v, unicode): | |
_row[k] = v.encode("utf-8") | |
else: | |
_row[k] = v | |
self.writer.writerow(_row) | |
# Fetch UTF-8 output from the queue ... | |
data = self.queue.getvalue() | |
data = data.decode("utf-8") | |
# ... and recode 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) | |
def writeheader(self): | |
self.writer.writeheader() | |
row1 = {'name':u'马克','pinyin':u'Mǎkè'} | |
row2 = {'name':u'美国','pinyin':u'Měiguó'} | |
f = open('out.csv','wb') | |
f.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly) | |
w = DictUnicodeWriter(f, sorted(row1.keys())) | |
w.writeheader() | |
w.writerows([row1,row2]) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment