我正在寻找一种方法来获取所提供的区域设置的货币名称。 例如:
currency_name = get_currency_name(locale = "en_US")
print(currency_name) --> "US Dollar"
一直在互联网上寻找答案,但找不到任何真正有用的东西。 这是我目前为止的情况:
import pycountry
country = pycountry.countries.get(name='Norway')
currency = pycountry.currencies.get(numeric=country.numeric)
currency_name = currency.name
print(currency_name) --> "Norwegian Krone"
谢谢!
最佳答案
import locale
import babel
from babel import numbers
def get_currency_name(loc):
l = locale.setlocale(locale.LC_ALL, '')
locale.setlocale(locale.LC_ALL, loc)
sym = locale.localeconv()['int_curr_symbol']
print(babel.numbers.get_currency_name(sym, locale='en_US'))
locale.setlocale(locale.LC_ALL, l)
get_currency_name('en_US')
get_currency_name('zh')
get_currency_name('de')
输出为
US Dollar
Chinese Yuan
Euro
关于python - 获取提供的区域设置的货币名称 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793512/