python - django 国家货币代码

标签 python django python-2.7 django-1.5 django-countries

我正在使用 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'

不知道这是否有效,想听听一些建议。

谢谢大家!

最佳答案

那里有几个模块:

  • pycountry :

    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
    
  • py-moneyed

    import moneyed
    
    country_name = 'France'
    
    for currency, data in moneyed.CURRENCIES.iteritems():
        if country_name.upper() in data.countries:
            print currency
            break
    

    打印 EUR

  • python-money

    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/

相关文章:

php - 是否有在 LAMP 堆栈上运行的 DotNetOpenAuth 等效项?

python - 如何将默认值从数据库传递到 wagtail 中的 map 字段面板

DJANGO:TemplateDoesNotExist:auth/user_confirm_delete.html

python - 通过不同的 python 脚本动态处理 getopt

python - 在 Pandas Dataframe 中删除行后获取错误的行数

Python3,对用户给定的元组求和

Python:删除某些字符之前的所有内容

python - 向 Django 模型添加代码和方法会破坏它

python - 使用python触发网站文件下载

python-2.7 - 为什么我的代码会陷入无限循环?