Strange problems with encoding

Joe Fromm jfromm at jasc.com
Thu Nov 6 09:00:23 EST 2003


"Sebastian Meyer" <s.meyer at technology-network.de> wrote in message
news:pan.2003.11.06.13.28.39.37248 at technology-network.de...
> Hi newsgroup,
>
> i am trying to replace german special characters in strings like
>     str = re.sub('ö', 'oe', str)
>
> When i work with this, i always get the message
> UniCode Error: ASCII decoding error : ordinal not in range(128)
>

Try adding

sys.setdefaultencoding( 'latin-1' )

to your site.py module, or rewrite your fragment as

from = 'ö'
to = 'oe'
s = re.sub( from.encode('latin-1'), to.encode('latin-1', s )

If you are running on Windows you might want to change 'latin-1' to 'mbcs',
as that seems to be the most forgiving codec, but it is Windows only.

Joe






More information about the Python-list mailing list