[Pythonmac-SIG] Accented characters and Python

João Leão joaoleao at gmx.net
Sat Oct 16 20:40:13 CEST 2004


Hi everyone

I'm having some trouble with accented characters (or non ascii 
characters).
Everything works fine when I just want to print or save to a file. For 
example:

 >>> s = 'á'  ------> accented a
 >>> print s
á                  ------> output is fine
 >>> s
'\x87\x8e'

Even when the string is read from a function like os.listdir(), the 
output with 'print' will work preserving the acented characters.

The problem comes when I have to pass this string to some function not 
so smart as 'print'.
For the sake of example, here is a small snippet of code that draws 
some text in a CoreGraphics's BitmapContext:

This will workfine:
_
s = '<some_accented_characters>'
c.showText (s, len(s))
_

But this won't work (the output will show other strange accented 
characters):
_
s = os.listdir('.')[1]   #suppose s is again a string of accented 
characters
c.showText (s, len(s))

This function is obviously using the wrong encodement for the output 
(the right one would be iso-8859-1, I suppose) and I can' figure a way 
to tell it which to use.
I found some articles about encodements but I don't really know what is 
the right thing to do. Then I tried several things like converting the 
string to unicode and then decode it to latin-1, but that was not the 
answer.

Thanks for any help.
_
joão



More information about the Pythonmac-SIG mailing list