[fix] resurrect csv output in py2

This commit is contained in:
Adam Tauber 2017-11-21 16:49:28 +01:00
parent 9ab8536479
commit 3d6c67951a
1 changed files with 13 additions and 9 deletions

View File

@ -29,6 +29,9 @@ except:
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
unichr = chr unichr = chr
unicode = str unicode = str
IS_PY2 = False
else:
IS_PY2 = True
logger = logger.getChild('utils') logger = logger.getChild('utils')
@ -159,19 +162,20 @@ class UnicodeWriter:
self.encoder = getincrementalencoder(encoding)() self.encoder = getincrementalencoder(encoding)()
def writerow(self, row): def writerow(self, row):
unicode_row = [] if IS_PY2:
for col in row: row = [s.encode("utf-8") if hasattr(s, 'encode') else s for s in row]
if type(col) == str or type(col) == unicode: self.writer.writerow(row)
unicode_row.append(col.encode('utf-8').strip())
else:
unicode_row.append(col)
self.writer.writerow([x.decode('utf-8') if hasattr(x, 'decode') else x for x in unicode_row])
# Fetch UTF-8 output from the queue ... # Fetch UTF-8 output from the queue ...
data = self.queue.getvalue().strip('\x00') data = self.queue.getvalue()
if IS_PY2:
data = data.decode("utf-8")
# ... and reencode it into the target encoding # ... and reencode it into the target encoding
data = self.encoder.encode(data) data = self.encoder.encode(data)
# write to the target stream # write to the target stream
self.stream.write(data.decode('utf-8')) if IS_PY2:
self.stream.write(data)
else:
self.stream.write(data.decode("utf-8"))
# empty queue # empty queue
self.queue.truncate(0) self.queue.truncate(0)