A question on Encoding and Decoding.

rbsharp at gmx.de rbsharp at gmx.de
Wed Nov 15 10:48:43 CET 2006

I think this remark is more to the point. In my experience, the general
problem is that python operates with the default encoding "ascii" as in
sys.getdefaultencoding(). It is possible to set the defaultencoding in
sitecustomize.py, with sys.setdefaultencoding('latin1'). I have placed
sitecustomize.py in Lib/site-packages. It is not possible to set the
encoding once python has started. Setting the encoding only works if
you can bind yourself to this one encoding and is therefore no general
The only reasonable way to work is to get your strings into unicode
(and sometimes back out again).
If for instance you type:
s = "äÄöÖüÜß" and then try
us = unicode(s) you will get a traceback identical to yours.
us = unicode(s,'latin1')
will work. If however to try:
print us
you will get another traceback.

>>> print "%r" % us
Even if it is not pretty, at least the program won't crash.
You can get a better result with:
>>> print us.encode('latin1')

The whole topic can get tedious at times, but be assured,
PyExcelerator, for writing, and xlrd for reading Excel files do work,
with unicode, both in the content and in the sheetnames. PyExcelerator
may be able to read Excel Sheets but not nearly as well as xlrd, which
works wonderfully.

Richard Sharp

Fredrik Lundh wrote:
> "kath" wrote:
> > Also python IDLE is able to output the same character corretly when I
> > say print and why not I?
> probably because IDLE's interactive window is Unicode-aware, but your terminal
> is not.
> </F>

More information about the Python-list mailing list