pycountry
pycountry copied to clipboard
Add mapping between currencies and their countries
Here https://www.iso.org/iso-4217-currency-codes.html they say:
"The alphabetic code is based on another ISO standard, ISO 3166, which lists the codes for country names. The first two letters of the ISO 4217 three-letter code are the same as the code for the country name, and, where possible, the third letter corresponds to the first letter of the currency name."
Thus I need to do:
currency_alpha3 = 'CNY'
country_alpha2 = currency_alpha3[0:2]
yuan = pycountry.currencies.get(alpha_3=currency_alpha3)
china = pycountry.countries.get(alpha_2=country_alpha2)
Would be better if I could use the library API to derive country by currency instead of writing the code above.
Also an opposite mapping could be done
I m not sure it is possible using just current jsons: iso3166-1.json and iso4217.json. For example, I want to see the currency for the France, the country is:
{
"alpha_2": "FR",
"alpha_3": "FRA",
"flag": "🇫🇷",
"name": "France",
"numeric": "250",
"official_name": "French Republic"
},
and for EUR:
{
"alpha_3": "EUR",
"name": "Euro",
"numeric": "978"
},
As you can see there is no common field. There should be another file with mapping country <> currency(s).
https://github.com/srcagency/country-currencies/blob/master/data.csv
@fertkir this data is available in the countryinfo library
>>> code = 'USA'
>>> import countryinfo
>>> usa_info = countryinfo.CountryInfo(code)
>>> usa_info.currencies()
['USD', 'USN', 'USS']