Commit 794f7588 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

snf_django: Fix csv unicode writer

Use recipe from to write unicode
in csv format.
parent 5aa0e3fa
# Adapted from
import csv
import codecs
import cStringIO
class UnicodeWriter(object):
"""CSV Unicode Writer
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",
errors='replace', **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds) = f
self.encoder = codecs.getincrementalencoder(encoding)(errors=errors)
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
# empty queue
def writerows(self, rows):
for row in rows:
......@@ -32,7 +32,6 @@
# or implied, of GRNET S.A.
import json
import csv
import operator
import locale
import unicodedata
......@@ -41,6 +40,7 @@ from datetime import datetime
from django.utils.timesince import timesince, timeuntil
from django.db.models.query import QuerySet
from django.utils.encoding import smart_unicode, smart_str
from import UnicodeWriter
def smart_locale_unicode(s, **kwargs):
......@@ -213,7 +213,8 @@ def pprint_table(out, table, headers=None, output_format='pretty',
out.write(json.dumps(table, indent=4))
elif output_format == "csv":
cw = csv.writer(out)
enc = locale.getpreferredencoding()
cw = UnicodeWriter(out, encoding=enc)
if headers:
table.insert(0, headers)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment