可能重複:
Python not sorting unicode properly. Strcoll doesn't help.重音字符的排序字符串在python
我想按字母順序排列的一些單詞排序。下面是我如何做到這一點:
#!/opt/local/bin/python2.7
# -*- coding: utf-8 -*-
import locale
# Make sure the locale is in french
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
print "locale: " + str(locale.getlocale())
# The words are in alphabetical order
words = ["liche", "lichée", "lichen", "lichénoïde", "licher", "lichoter"]
for word in sorted(words, cmp=locale.strcoll):
print word.decode("string-escape")
我期待這樣的詞語都印在同一順序它們被定義,但這裏是我得到:
locale: ('fr_FR', 'UTF8')
liche
lichen
licher
lichoter
lichée
lichénoïde
的é字符被視爲大於z。
看來我誤解了locale.strcoll
如何比較字符串。我應該使用什麼比較函數來獲取按字母順序排序的單詞?
它看起來像正常工作。我看不到「z」。 – 2010-12-10 13:03:13
在這個例子中沒有'z',但'é'在'o'之後,這不是字母順序。 – 0xced 2010-12-10 13:06:12
在2.6.4中正常工作。 – 2010-12-10 13:09:19