如何使用python中的locale通过给定的货币alpha-3代码名获得对应的货币符号?在
我正在做:
import locale, re
def get_currency_symbol(alpha_3='USD'):
locales_all = locale.locale_alias.values()
for l in locales_all:
try:
try:
locale.setlocale(locale.LC_ALL, l)
except NameError:
l = re.sub('\..*$','.utf8',l) # On Linux the file are normally
# finished with '.utf8'.
locale.setlocale(locale.LC_ALL, l)
conv=locale.localeconv()
if conv['int_curr_symbol'] == alpha_3.upper():
return conv['currency_symbol']
except:
pass
return None
但我对地区名称有问题:
locale.Error: unsupported locale setting
setlocale()
的文档指出,如果给定一个locale参数,它应该是区域设置的名称,或者是一个包含别名两部分的iterable。 因此,要么将获取区域设置的行更改为:或者将其设置为:
^{pr2}$(但不要同时做这两件事:)
第一种方法更好,第二种方法可能需要更精细的方法来处理所有的语言环境别名。在
更多信息请访问documentation
相关问题 更多 >
编程相关推荐