Skip to content

Instantly share code, notes, and snippets.

@steinnes
Created April 6, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinnes/ae2294e6003d39c404cdbdb89a7dbb72 to your computer and use it in GitHub Desktop.
Save steinnes/ae2294e6003d39c404cdbdb89a7dbb72 to your computer and use it in GitHub Desktop.
import sys
UTF8Writer = codecs.getwriter('utf8')
class SafeStdoutWriter(UTF8Writer):
""" A safe stdout writer class which expects to output only unicode string objects
but if it encounters a decoding error (because the given string is not unicode but
a normal 'str') it will attempt to convert it to unicode and try again. If that
conversion fails we fall back to decoding iso-8859-1 which is the default codec
the CSVOutput writer uses.
"""
def encode(self, obj, errors):
try:
return UTF8Writer.encode(obj, errors)
except UnicodeDecodeError:
try:
return UTF8Writer.encode(obj.decode('utf-8'), errors)
except UnicodeDecodeError:
return UTF8Writer.encode(obj.decode('iso-8859-1'), errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment