469,306 Members | 1,881 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

sort accented string

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.
Jul 18 '05 #1
5 4208
Laurent wrote:
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("abfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter

Jul 18 '05 #2
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 Otten wrote:
Laurent wrote:
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("abfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter


Jul 18 '05 #3
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 Otten wrote:
Laurent wrote:
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("abfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter


Jul 18 '05 #4
Laurent wrote:
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

Jul 18 '05 #5
Peter Otten wrote:
Laurent wrote:

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

Note however that you need to transform both strings being compared...
e.g., with a french locale:
'd' < '\xe9' True 'f' < '\xe9' True 'd' < locale.strxfrm('\xe9') False 'f' < locale.strxfrm('\xe9') False locale.strxfrm('f') < locale.strxfrm('\xe9') False locale.strxfrm('d') < locale.strxfrm('\xe9')

True

David
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Al Newton | last post: by
8 posts views Thread by lok | last post: by
1 post views Thread by anonieko | last post: by
29 posts views Thread by zoro | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
12 posts views Thread by aparnakakkar2003 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.