Python-将货币代码转换为其符号 [英] Python - Convert currency code to its sign

查看:657
本文介绍了Python-将货币代码转换为其符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何将货币代码转换为符号?

In Python, how can I convert currency code to its sign?

例如, USD 将是转换为 $ ,而 JPY 将转换为¥

For example, USD would be converted to $, and JPY would be converted to ¥.

如果没有通用的方法可以在网上找到这些字典的简单词典?

If there isn't a generic way to do this, is there any simple dictionary of these on the Web?

谢谢。

推荐答案

使用语言环境模块:

import locale

locales=('en_AU.utf8', 'en_BW.utf8', 'en_CA.utf8',
    'en_DK.utf8', 'en_GB.utf8', 'en_HK.utf8', 'en_IE.utf8', 'en_IN', 'en_NG',
    'en_PH.utf8', 'en_US.utf8', 'en_ZA.utf8',
    'en_ZW.utf8', 'ja_JP.utf8')
for l in locales:
    locale.setlocale(locale.LC_ALL, l)
    conv=locale.localeconv()
    print('{ics} ==> {s}'.format(ics=conv['int_curr_symbol'],
                                 s=conv['currency_symbol']))

收益率:

AUD  ==> $
BWP  ==> Pu
CAD  ==> $
DKK  ==> kr
GBP  ==> £
HKD  ==> HK$
EUR  ==> €
INR  ==> ₨
NGN  ==> ₦
PHP  ==> Php
USD  ==> $
ZAR  ==> R
ZWD  ==> Z$
JPY  ==> ¥

请注意,您需要在计算机上安装区域设置信息。在Ubuntu上,这意味着已安装正确的 language-pack-* 软件包。

Note you need the locale information installed on your machine. On Ubuntu, this means having the right language-pack-* packages installed.

在* nix系统上,您可以使用

On *nix systems, you can find the list of known locales (e.g. en_GB.utf8) with

locale -a

我不知道从Python内部获取此列表的方法(没有使用子流程)。

I don't know of a way to obtain this list from within Python (without using subprocess).

这篇关于Python-将货币代码转换为其符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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