[Tutor] Newbie self-introduction + little encoding problem

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Dec 29 16:53:16 EST 2003


> The program I've written works perfectly if I use no French accents
at all.
> However, if I add accents at "Février", "Août", and "Décembre", as
it should
> be the case to write months' names in French, here is what I obtain
when I
> run the program :
>
> >>>
> [31, 'Janvier', 28, 'F\xe9vrier', 31, 'Mars', 30, 'Avril', 31,
'Mai', 30,
> 'Juin', 31, 'Juillet', 31, 'Ao\xfbt', 30, 'Septembre', 31,
'Octobre', 30,
> 'Novembre', 31, 'D\xe9cembre']
> >>>

That will actually work OK when you try to print it, it's just that
Python is showing up the accented characters in their
hexadecimal form. Try this:

dates =  [31, 'Janvier', 28, 'F\xe9vrier', 31, 'Mars', 30, 'Avril',
31, 'Mai', 30,
 'Juin', 31, 'Juillet', 31, 'Ao\xfbt', 30, 'Septembre', 31, 'Octobre',
30,
 'Novembre', 31, 'D\xe9cembre']

for index in range(0,len(dates),step=2):
    print dates[index], '  ', dates[index+1]


You should find the printed names are as you expect.

This is due to a subtlty in the way the python interpreter works.
If you evaluate an expression at the >>> prompt python uses the
repr() function to display it. By convention repr() is intended to
assist programmers and shows a data centric view of the data,
whereas the print command uses the str() representation which
is aimed at end users and shows a more user friendly representation.
For many things repr() and str() are the same but for somne
things - like unicode strings - they are different.

TO prove the point lets look at the simpler case of:

>>> s = 'F\xe9vrier'
>>> s
'F\xe9vrier'

Notice both the quote marks and the hex substitution.

>>> print s
Février

No quotes and the accented character.
Finally look at these and think about why they come out as they do...

>>> repr(s)
"'F\\xe9vrier'"
>>> str(s)
'F\xe9vrier'
>>> print repr(s)
'F\xe9vrier'
>>> print str(s)
Février
>>>

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list