Unicode encoding - ignoring errors
mludvig at logix.net.nz
Mon Dec 29 13:45:00 CET 2008
Chris Rebert wrote:
> On Mon, Dec 29, 2008 at 4:06 AM, Michal Ludvig <mludvig at logix.net.nz> wrote:
>> in my script I have sys.stdout and sys.stderr redefined to output
>> unicode strings in the current system encoding:
>> encoding = locale.getpreferredencoding()
>> sys.stdout = codecs.getwriter(encoding)(sys.stdout)
>> However on some systems the locale doesn't let all the unicode chars be
>> displayed and I eventually end up with UnicodeEncodeError exception.
>> I know I could explicitly "sanitize" all output with:
>> whatever.encode(encoding, "replace")
>> but it's quite inconvenient. I'd much prefer to embed this "replace"
>> operation into the sys.stdout writer.
> codecs.getwriter() returns a StreamWriter subclass (basically).
> The constructor of said subclass has the signature:
> StreamWriter(stream[, errors])
> You want the 'errors' argument.
(and I'm going to read the module docs, really ;-)
More information about the Python-list