Home > Mobile >  Python library to convert full currency names to their appropriate abbreviation
Python library to convert full currency names to their appropriate abbreviation

Time:10-14

I have a list of currencies as:

       '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'

Are their any existing libraries that can help me convert these names to their proper abbreviations For example:

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

CodePudding user response:

You can make use of this JSON data to find the abbreviations and symbols https://gist.github.com/stevekinney/8334552. Through this, you can iterate through the data and can easily find the symbol and abbreviation with their names.

CodePudding user response:

Yes, This Library iso4217parse

For instance-

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])

Output-

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

Although, test as to what output it gives for a few currencies, rarely it gave a different currency for a symbol with regard to the project requirements.

  • Related