Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58080
| Date | 2013-10-30 19:38 +0100 |
|---|---|
| From | Ulrich Goebel <ml@fam-goebel.de> |
| Subject | sorting german characters äöü... |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1823.1383158355.18130.python-list@python.org> (permalink) |
Hello,
for a SQLite database I would like to prepare a collating function in
python. It has to compare two (unicode-)strings s, t and should return
-1 if s<t, 0 if s=t and 1 if s>t.
The strings are german names/words, and what I would like is to have a
case-insensitive ordering, which treates
ä as a
ö as ö
ü as ü
ß as ss
and a few more things.
What I did is to "normalize" the two strings and then compare them:
def normal (s):
r = s
r = r.strip() # wir entfernen führende und folgende Leerzeichen
r = r.replace(u' ', '') # wir entfernen alle Leerzeichen innerhalb
r = r.replace(u'ß', u'ss')
r = r.upper() # alles in Großbuchstaben
r = r.replace(u'Ä', u'A') # Umlaute brauchen wir nicht...
r = r.replace(u'Ö', u'O') # "
r = r.replace(u'Ü', u'U') # "
return r
def compare (a, b):
aa = normal(a)
bb = normal(b)
if aa < bb:
return -1
elif aa == bb:
return 0
else:
return 1
That works, but my be there is a more intelligent way? Especially there
could be much more r.replace to handle all the accents as ^ ° ´ ` and so on.
Any ideas? That would be great!
Ulrich
--
Ulrich Goebel
Paracelsusstr. 120, 53177 Bonn
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
sorting german characters äöü... Ulrich Goebel <ml@fam-goebel.de> - 2013-10-30 19:38 +0100
csiph-web