write Unicode to sys.out.write without access to sitecustomize.py

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Aug 28 11:22:49 EDT 2009


On 02:51 pm, rknop at pobox.com wrote:
>
>I would like to tell the system that it's OK to write Unicode to 
>sys.out
>and sys.err.  However, I'm doing this in a CGI script where I don't 
>have
>access to the system directories, and as such can't use
>sys.setdefaultencoding in sitecustomize.py.
>
>Is there a way to make this happen?

Sure, just replace the two files with versions that know how to encode 
the unicode using the correct encoding:

import sys, codecs
info = codecs.lookup('utf-8') # for example
sys.stdout = info.streamwriter(sys.stdout)
sys.stderr = info.streamwriter(sys.stderr)

Jean-Paul



More information about the Python-list mailing list