How to get Python to default to UTF8
Fredrik Lundh
fredrik at pythonware.com
Sat Dec 22 08:14:36 EST 2007
weheh wrote:
> Hi Fredrik, thanks for responding. After reading up some more on this, I
> think my title should be changed to "How to get Python to default to ASCII".
> In point of fact, I want my 2 environments to agree so that I can debug
> thinkgs more easily. Right now it's a nightmare.
>
> As to your questions, in this example, I believe the exception was caused by
> trying to do a count of the number of times a string appears in an array.
> One of the strings was unicode and the other was encoded by Python by
> default.
to fix this, figure out from where you got the encoded (8-bit) string,
and make sure you decode it properly on the way in. only use Unicode
strings on the "inside".
(Python does have two encoding defaults; there's a default encoding that
*shouldn't* ever be changed from the "ascii" default, and there's also a
stdin/stdout encoding that's correctly set if you run the code in an
ordinary terminal window. if you get your data from anywhere else, you
cannot trust any of these, so you should do your own decoding on the way
in, and encoding things on the way out).
</F>
More information about the Python-list
mailing list