sort accented string

L

Laurent

Hello,

I'm french and I have a small sorting problem with python (and zope's
zcatalog):

In a python shell python, try :

'é' > 'z'

The answer is true. Then if you try

test=['a','b','f','é','z']
print test.sort()

You almost get :

['a','b','f','z','é']

That's not what we want :)

Any help will be greatly appriciated.

PS : I got the same behaviour with zope's zcatalog during a sort_on request.
Even if I start zope with the correct linux locale (fr_FR).

THX

Laurent.
 
L

Laurent

Thank you very much Peter, that's a really great tip. You should try to
publish this one on the Internet 'cause I didn't find any web site
explaining this.

Bye.

Laurent

Peter said:
Laurent said:
I'm french and I have a small sorting problem with python (and zope's

Try locale.strcoll():
import locale
locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR'
test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9']
test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']

Peter
 
L

Laurent

Peter what about using this kind of tip with cmp() built-in ??

def myCompare(self,a,b):
cmp(a,b)

with a and b being french accented chars ???

Any idea ??

Thanks again.

Peter said:
Laurent said:
I'm french and I have a small sorting problem with python (and zope's

Try locale.strcoll():
import locale
locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR'
test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9']
test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']

Peter
 
P

Peter Otten

Laurent said:
Peter what about using this kind of tip with cmp() built-in ??

def myCompare(self,a,b):
cmp(a,b)

with a and b being french accented chars ???

Any idea ??

From the documentation of the locale module:

"""
strxfrm(string)

Transforms a string to one that can be used for the built-in function cmp(),
and still returns locale-aware results. This function can be used when the
same string is compared repeatedly, e.g. when collating a sequence of
strings.
"""

Peter
 
D

David Fraser

Peter said:
Laurent wrote:




From the documentation of the locale module:

"""
strxfrm(string)

Transforms a string to one that can be used for the built-in function cmp(),
and still returns locale-aware results. This function can be used when the
same string is compared repeatedly, e.g. when collating a sequence of
strings.
"""

Peter
Note however that you need to transform both strings being compared...
e.g., with a french locale:
True

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top