[Tutor] re.sub

Gregor Lingl glingl@aon.at
Mon, 19 Aug 2002 19:29:42 +0200


This is a multi-part message in MIME format.
--------------060905000305000409070301
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Nicole.Seitz@urz.uni-hd.de schrieb:

>Hi there!
> Today I got the following exception after inserting re.sub("ü","ü",text)
>
>somewhere in my program.
>
>
>Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
>...
>  File "C:\PROGRA~1\Tools\idle\IOBinding.py", line 154, in writefile
>    f.write(chars)
>UnicodeError: ASCII encoding error: ordinal not in range(128)
>
>Can anybody tell my why there's a problem with the German umlaut?
>  
>
This is an ugly IDLE-"feature". IDLE uses (and allows) only characters 
with ASCII-code < 128
As the error-message says, Umlaute hav ASCII-codes not in this range:

 >>> map(ord,"äöüÄÖÜß")
[228, 246, 252, 196, 214, 220, 223]
 >>>

You may change this by informing IDLE/Python about your local environment.
To this end put the attached file

sitecustomize.py

into the Lib-subdirectory of your Python-Directory (standard: 
C:\Python22\Lib).
Restart IDLE and the problem will be disappeared.

If there could arise other new problems because of  changing the "locale"
(see global module-Index, locale.py) I don't know. I didn't encounter  any.

Best wishes
Gregor

--------------060905000305000409070301
Content-Type: text/plain;
 name="sitecustomize.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="sitecustomize.py"

import sys, locale
loc = locale.getdefaultlocale()
if loc[1]:
    encoding = loc[1]
if encoding != "ascii":
    sys.setdefaultencoding(encoding)
--------------060905000305000409070301--