making 'utf-8' default codec

Scott David Daniels scott.daniels at acm.org
Mon Feb 27 11:28:43 EST 2006


Nikola Skoric wrote:
> Is there a way of making 'utf-8' default codec for the whole program, so 
> I don't have to do .encode('utf-8') every time I print out a string?

Explicit is better than implicit (so setting up a default codec is
considered bad practice).  However, you could wrap an output destination
with an encoder and get the effect you want.

     import sys, codecs
     sys.stdout, _held = (codecs.getwriter('utf-8')(sys.stdout),
                          sys.stdout)

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list