Python string.letters 不包括语言环境变音符号

Python string.letters does not include locale diacritics

我正在尝试根据给定的语言环境从 python 字符串模块获取字母表,但没有成功(即使用变音符号,即法语的 éèêà...)。这是一个最小的例子:

import locale, string

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
print string.letters
# shows ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
print string.letters
# also shows ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

python文档中说string.letters is locale dependent,但好像对我不起作用

我做错了什么,这是获得语言相关字母表的正确方法吗?

编辑:我刚刚在设置后检查了语言环境 print locale.getlocale(),它已正确更改。

在 python 2.7 中(python 3.x 中没有 string.letters)如果您将语言环境设置为 'fr_FR'(相当于 'fr_FR.ISO8859-1', 而不是 'fr_FR.UTF-8').

>>> import locale, string
>>> locale.setlocale(locale.LC_ALL, 'es_ES')
'es_ES'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\xaa\xb5\xba\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
>>> locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')
'es_ES.UTF-8'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

所以\xaa是字符“ª”,\xab是“«”,\xd1是“Ñ”等等。但是编码表示确实坏了。

我强烈推荐阅读这篇文章:https://pythonhosted.org/kitchen/unicode-frustrations.html