ala*_*arr 12 python python-2.7
下面的代码忽略了语言环境,Égypt最后会出现什么问题?
dict = {"United States": "United States", "Spain" : "Spain", "England": "England", "Égypt": "Égypt"}
import locale
# using your default locale (user settings)
locale.setlocale(locale.LC_ALL,"fr_FR")
print OrderedDict(sorted(dict.items(), key=lambda t: t[0], cmp=locale.strcoll))
Run Code Online (Sandbox Code Playgroud)
这是输出:
OrderedDict([('England', 'England'), ('Spain', 'Spain'), ('United States', 'United States'), ('\xc3\x89gypt', '\xc3\x89gypt')])
Run Code Online (Sandbox Code Playgroud)
Dim*_*nek -1
这是一个解决方法。
使用 unicode 的规范化形式规范分解http://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms
# utf-8 <-> unicode is left as exercise to the reader
egypt = unicodedata.normalize("NFD", egypt)
sorted(['Egypt', 'E\xcc\x81gypt', 'US'])
['Egypt', 'E\xcc\x81gypt', 'US']
Run Code Online (Sandbox Code Playgroud)
这实际上并没有考虑区域设置。
除此之外,尝试使用较新的 Python(是的,我知道)或来自 Martijn 链接问题和相应答案的 ICU 库。
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |