making 'utf-8' default codec

N

Nikola Skoric

Hi there,

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?
 
J

Jarek Zgoda

Nikola Skoric napisa³(a):
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?

Bad idea. You may accidentally break some libraries that depend on ASCII
being default & standard.
 
S

Scott David Daniels

Nikola said:
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
(e-mail address removed)
 
J

Jorge Godoy

Jarek Zgoda said:
Bad idea. You may accidentally break some libraries that depend on ASCII
being default & standard.

And what would those produce as output when fed with unicode data? How would
they handle this input? IMVHO nothing should rely on having a standard
charset as input. If it is required, then the library should set it up by
itself: explicit is better than implicit.

--
Jorge Godoy <[email protected]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
 
J

Jarek Zgoda

Jorge Godoy napisa³(a):
And what would those produce as output when fed with unicode data? How would
they handle this input? IMVHO nothing should rely on having a standard
charset as input. If it is required, then the library should set it up by
itself: explicit is better than implicit.

Besides that, changing default encoding will make program nearly
absolutely unportable as Python by default installs nearly everywhere
with ASCII as default encoding (with iSeries being the only exception
known to me).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top