我正在使用 django_countries
来显示国家列表。现在,我有一个要求,我需要根据国家/地区显示货币。
挪威 - 挪威克朗、欧洲和非洲(英国除外) - 欧元、英国 - 英镑、美洲和亚洲 - 美元。
这可以通过 django_countries 项目实现吗?或者我可以使用 python 或 django 中的任何其他包吗?
也欢迎任何其他解决方案。
------------------------更新------------
在得到很多解决方案之后,主要强调的是:
挪威 - 挪威克朗、欧洲和非洲(英国除外) - 欧元、英国 - 英镑、美洲和亚洲 - 美元。
---------------------------- 解决方案---------------- --------------
我的解决方案非常简单,当我意识到我无法获得任何 ISO 格式或软件包来获得我想要的东西时,我想到编写自己的脚本。这只是一个基于条件的逻辑:
from incf.countryutils import transformations
def getCurrencyCode(self, countryCode):
continent = transformations.cca_to_ctn(countryCode)
# print continent
if str(countryCode) == 'NO':
return 'NOK'
if str(countryCode) == 'GB':
return 'GBP'
if (continent == 'Europe') or (continent == 'Africa'):
return 'EUR'
return 'USD'
不知道这是否有效,想听听一些建议。
谢谢大家!
最佳答案
那里有几个模块:
-
import pycountry country = pycountry.countries.get(name='Norway') currency = pycountry.currencies.get(numeric=country.numeric) print currency.alpha_3 print currency.name
打印:
NOK Norwegian Krone
-
import moneyed country_name = 'France' for currency, data in moneyed.CURRENCIES.iteritems(): if country_name.upper() in data.countries: print currency break
打印
EUR
-
import money country_name = 'France' for currency, data in money.CURRENCY.iteritems(): if country_name.upper() in data.countries: print currency break
打印
EUR
pycountry
定期更新,py-moneyed
看起来不错,比 python-money
功能更多,加上 python-money
现在不维护了。
希望对您有所帮助。
关于python - django 国家货币代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966592/