[spe.pycs.net] Unicode Encoding?

Benno Dielmann mail at benno-dielmann.de
Tue Nov 23 10:12:51 CET 2004


On Tuesday 23 November 2004 06:34, Chris Spencer wrote:
| I'm trying to embed Japanese characters in my source using Unicode, and
| SPE keeps giving me: UnicodeEncodeError: 'latin-1' codec can't encode
| characters in position 1530-1531: ordinal not in range(256). How do you
| change SPE's encoding to utf-8 or utf-16? This problem caught me
| off-guard because I was able to embedded unicode just fine using IDLE
| (it even displays correctly), but then it broke when I reloaded the file
| in SPE. SPE is a great editor, let's not give IDLE bragging rights over it.

Hi Chris, 

I'll paste in my solution to this problem which I posted to this mailing list 
some time ago. So here it comes:

-----------------------

In fact it probably hasn't anything to do with the characters in the directory 
name but with that ones in your file. 

The reason is a "feature" in the wxStyledTextControl from wxPython Spe uses. 
This text control always tries to save files with the python standard 
encoding which happens to be ascii out of the box. If your file isn't ascii 
encoded (e.g. if you are using non-ascii chars in comments or strings), you 
get the above error. 

Perhaps the problem arises as well with non-ascii directory names, but I 
haven't tested that. 

The solution I found will probably work any way so here is it:

In the "site-packages" directory in my python dir 
(/usr/lib/python/site-packages on my linux box) I created a file named 
"sitecustomize.py" with the following content:

-----------------
import sys

sys.setdefaultencoding('iso-8859-1')
-----------------

This script is run automatically on python startup and sets the default 
encoding to 'iso-8859-1'. Please insert your appropriate encoding, e.g. 
'utf-8'. 

Everything should work fine now. 

------------------------

Have fun, 

Benno. 



More information about the Spe-user mailing list