我试图从python字符串模块中获取字母表,这取决于给定的区域设置,但没有成功(也就是使用diacritics,即.代表法语)。下面是一个很小的例子:
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依赖于地区,但它似乎对我不起作用。
我做错了什么,这是获得一个语言依赖的字母的正确方式吗?
编辑:我刚刚检查了地区print locale.getlocale()设置后,它是正确的改变。
发布于 2016-10-26 11:58:42
在python2.7中(python3.x中没有string.letters ),如果将区域设置为'fr_FR‘(等效于’fr_FR.iso 8859-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是字符“and”、\xab“”、\xd1 1是“is”等等。但是编码表示确实被打破了。
我确实强烈推荐阅读以下内容:https://pythonhosted.org/kitchen/unicode-frustrations.html
https://stackoverflow.com/questions/40259024
复制相似问题