如何在Python 3中获取当前语言环境的字母? [英] How to get the current locale's alphabet in Python 3?

查看:80
本文介绍了如何在Python 3中获取当前语言环境的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2中,您可以执行以下操作来获取当前语言环境的字符集:

In Python 2 you could do the following to get the current locale's character set:

import string
print string.letters

但是,在Python 3中,字符串模块的语言环境相关常量(例如, string.letters string.lowercase string.uppercase ,等).

However, in Python 3 the string module's locale-dependent constants (e.g. string.letters, string.lowercase, string.uppercase, etc.) were removed.


如何使用Python 3获取当前语言环境的字符集?


How can I get the current locale's character set using Python 3?

推荐答案

您可以获取 pyicu 模块为每个区域设置html"rel =" noreferrer>示例字符:/p>

You can get the exemplar characters for each locale using the pyicu module:

import locale
from icu import LocaleData

default, encoding = locale.getdefaultlocale()
languages = [default] + ['en_US', 'fr_FR', 'es_ES']

for language in languages:
    data = LocaleData(language)
    alphabet = data.getExemplarSet()
    print(language, alphabet)

输出

pt_BR [a-zà-ãçéêíò-õú]
en_US [a-z]
fr_FR [a-zàâæ-ëîïôùûüÿœ]
es_ES [a-záéíñóúü]

要获取当前的语言环境就足够了:

To get the current locale is enough to do:

default, _ = locale.getdefaultlocale()
data = LocaleData(default)
alphabet = data.getExemplarSet()
print(default, alphabet)

这篇关于如何在Python 3中获取当前语言环境的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆