print u"\u0432": why is this so hard? UnciodeEncodeError

Nelson Minar nelson at monkey.org
Thu Apr 8 11:52:01 EDT 2004


Thanks for your answers.

"Martin v. Löwis" <martin at v.loewis.de> writes:
>As you have discovered, this is not so simple. Printing this character
>might not be possible at all

I know. I'm just trying to figure out an expedient way to get Python
to make a best effort.

> When you redirect the output to a file, it is not a terminal anymore,
> and Python cannot guess the encoding.

So when Python can't guess the encoding, it assumes that ASCII is the
best it can do? Even as an American that annoys me; what do folks who
need non-ASCII do in practice? Martin, what do you do when you write a
Python script that prints your own name?

I guess what I'd like is a way to set Python's default encoding and
have that respected for files, terminals, etc. I'd also like some way
to override the Unicode error mode. 'strict' is the right default, but
I'd like the option to do 'ignore' or 'replace' globally.

> Assigning sys.stdout is the right thing to do. I'm uncertain why
> that could be an awful lot of work, as you do this only once...

Now that I know the trick I can do it. But part of the joy of Python
is that it makes simple things simple. For a beginner to the language
having to learn about the difference between sys.stdout and
sys.__stdout__ seems a bit much.



More information about the Python-list mailing list