Python 将完整货币名称转换为适当缩写的库

Python library to convert full currency names to their appropriate abbreviation

我有一个货币列表:

       'United States dollar', 'European Euro', 'Indian rupee',
       'Pound sterling', 'Canadian dollar', 'Brazilian real',
       'Australian dollar', 'Polish zloty', 'Russian ruble', 'Swedish krona',
       ...
       'Macanese pataca', 'Samoan tala', 'Libyan dinar', 'Namibian dollar',
       'Cayman Islands dollar', 'Fijian dollar', 'Lesotho loti',
       'Turkmen manat', 'Somali shilling', 'Mozambican metical'

他们是否有任何现有的库可以帮助我将这些名称转换为正确的缩写 例如:

United States dollar -- USD
European Euro -- EUR
Indian Rupee -- INR etc

是的,这个图书馆 iso4217parse

比如-

a = 'United States dollar'
text = iso4217parse.by_symbol_match(a.strip())

text
[Currency(alpha3='USD', code_num=840, name='United States dollar', 
symbols=['US$', '$', '$', '﹩', 'dollar', 'dollars', 'Dollar', 
'Dollars', 'US$', 'US﹩'], minor=2, countries=['AS', 'BB', 'BM', 'BQ', 
'EC', 'FM', 'GU', 'HT', 'IO', 'MH', 'MP', 'PA', 'PR', 'PW', 'SV', 'TC', 
'TL', 'UM', 'US', 'VG', 'VI'])]

currency = str(text[0][3][1])+' '+str(text[0][2])

输出-

'$ United States dollar' 
 # in the current case, the text variable will need
 # to be parsed in order to get alpha3='USD'

尽管,测试它为几种货币提供的输出,很少根据项目要求为符号提供不同的货币。

您可以使用此 JSON 数据来查找缩写和符号 https://gist.github.com/stevekinney/8334552。 通过这个,您可以遍历数据并可以轻松找到带有名称的符号和缩写。