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 http://docs.python.org/2/library/csv.html to write unicode
in csv format.
parent 5aa0e3fa
# Adapted from http://docs.python.org/2/library/csv.html
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)
self.stream = 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
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
import json import json
import csv
import operator import operator
import locale import locale
import unicodedata import unicodedata
...@@ -41,6 +40,7 @@ from datetime import datetime ...@@ -41,6 +40,7 @@ from datetime import datetime
from django.utils.timesince import timesince, timeuntil from django.utils.timesince import timesince, timeuntil
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.utils.encoding import smart_unicode, smart_str from django.utils.encoding import smart_unicode, smart_str
from snf_django.management.unicodecsv import UnicodeWriter
def smart_locale_unicode(s, **kwargs): def smart_locale_unicode(s, **kwargs):
...@@ -213,7 +213,8 @@ def pprint_table(out, table, headers=None, output_format='pretty', ...@@ -213,7 +213,8 @@ def pprint_table(out, table, headers=None, output_format='pretty',
out.write(json.dumps(table, indent=4)) out.write(json.dumps(table, indent=4))
out.write("\n") out.write("\n")
elif output_format == "csv": elif output_format == "csv":
cw = csv.writer(out) enc = locale.getpreferredencoding()
cw = UnicodeWriter(out, encoding=enc)
if headers: if headers:
table.insert(0, headers) table.insert(0, headers)
cw.writerows(table) cw.writerows(table)
......
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