Is there a way to change the default string encoding?

Peter Otten __peter__ at web.de
Tue Aug 21 02:16:27 EDT 2007


Ron Garret wrote:

> Is there a way to change the default string encoding used by the
> string.encode() method?  

encode() or decode()? Encoding is best handled by the output stream, e. g.
passing codecs.open(...) instead of the builtin open(...).


> My default environment is utf-8 but I need it 
> to be latin-1 to avoid errors like this:
> 
>>>> 'Andr\xe9 Ramel'.decode()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 4:
> ordinal not in range(128)

If your environment were latin-1, you'd get the same error because Python
assumes ascii by default.
 
> I can't change the code to pass an encoding argument to the decode
> method because it's someone else's code.

Does that code accept unicode strings? Try to pass u"Andre\xe9 Ramel"
instead of the byte string.

If all else fails there's

>>> sys.setdefaultencoding("latin1")
>>> "Andre\xe9 Ramel".decode()
u'Andre\xe9 Ramel'

but that's an evil hack, you should rather talk to the maintainer of the
offending code to update it to accept unicode.

Peter



More information about the Python-list mailing list