可能重复:
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