PyCountry currency formatting woes

305 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
May 4, 2014, 4:25:05 PM5/4/14
to Python, django...@googlegroups.com
Hi everyone,

I have a function which accepts an alpha2 country code and a price string, where the aim is to get the country's currency and use the currency.letter property of that currency to format the supplied price string.

The above works fine so far - yet it falls over when called with Germany as the country as follows:

currency = pycountry.currencies.get(numeric=country.numeric)

The function implementation is as follows:

def formatPrice(self, alpha2CountryCode, price):
        """
        @param alpha2CountryCode: The 2-character country code for which to format the price value
        @param price: The price value as a string
        @return: A string representing the formatted monetary value for this country for this price.

        #Get country by alpha2 code
        country = pc.countries.get(alpha2=alpha2CountryCode.upper())

        #Get currency by country's numeric and format price
        currency = pc.currencies.get(numeric=country.numeric)
        letter = currency.letter
        formattedCurrency = "%s %s" % (letter, price)

        return formattedCurrency

Any ideas as to what the issue may be?

Thanks :)

--
Regards,
Sithu Lloyd Dube

Sithembewena Lloyd Dube

unread,
May 4, 2014, 4:55:50 PM5/4/14
to Python, django...@googlegroups.com
Thanks, i was actually getting the error information to update the post. Apoligies to waste your time posting here - I could not find an appropriate PyCountry discussion list and my next best bet seemed to be a Python users' list.

For those who care to look, the error is as follows (a concise example from an interactive shell:

import pycountry
country = pycountry.countries.get(alpha2='DE')
currency = pycountry.currencies.get(numeric=country.numeric)
Traceback (most recent call last):
File "", line 1, in 
File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
return self.indices[field][value]
KeyError: '276'

The obvious issue here is that the pycountry.countries collection does not contain a currency with a numeric of 276 (Germany's numeric) - yet it does contain the Euro. Any ideas as to what the way around this may be?

Sithembewena Lloyd Dube

unread,
May 6, 2014, 3:15:33 AM5/6/14
to Python, django...@googlegroups.com
Thanks for this response, this is exactly what I needed to know.


On Mon, May 5, 2014 at 6:26 AM, Marc Tompkins <marc.t...@gmail.com> wrote:
On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube <zeb...@gmail.com> wrote:
Thanks, i was actually getting the error information to update the post. Apoligies to waste your time posting here - I could not find an appropriate PyCountry discussion list and my next best bet seemed to be a Python users' list.


You also posted on StackOverflow; I just answered you there.  In short: the currency numeric is not guaranteed to be the same as the country numeric (in the case of the Euro, how could it possibly be?)  The numeric for DE is 276; the numeric for the Euro is 978.  Obviously they don't match.

PyCountry is a wrapper around some tables provided by Debian; those tables don't include a country/currency mapping.  You can find those mapping tables at
     http://www.currency-iso.org/en/home/tables/table-a1.html
and roll your own wrapper; I'm sure it's been done a thousand times before, but I'm not aware of a Python package that does this.

Sithembewena Lloyd Dube

unread,
May 8, 2014, 10:02:07 AM5/8/14
to Python, django...@googlegroups.com
Thank you all, babel works just fine. I also tried ccy, which isn't bad either - except that it returns non-unicode currency letters for countries in the Eurozone.


On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__pe...@web.de> wrote:
Sithembewena Lloyd Dube wrote:

> Thanks, i was actually getting the error information to update the post.
> Apoligies to waste your time posting here - I could not find an
> appropriate PyCountry discussion list and my next best bet seemed to be a
> Python users' list.
>
> For those who care to look, the error is as follows (a concise example
> from an interactive shell:
>
> import pycountry
> country = pycountry.countries.get(alpha2='DE')
> currency = pycountry.currencies.get(numeric=country.numeric)
> Traceback (most recent call last):
> File "", line 1, in
> File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
> return self.indices[field][value]
> KeyError: '276'
>
> The obvious issue here is that the pycountry.countries collection does not
> contain a currency with a numeric of 276 (Germany's numeric) - yet it does
> contain the Euro. Any ideas as to what the way around this may be?

It looks like the development version of babel

http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies

can do what you want:

$ LANG=en_US.UTF-8 python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import babel.numbers as bn
>>> bn.get_territory_currencies("DE")
['EUR']
>>> print bn.format_currency(1.234, "EUR")
€1.23
>>> print bn.format_currency(1.234, "EUR", locale="DE")
1,23 €
>>> import babel
>>> babel.__version__
'2.0-dev'

_______________________________________________
Tutor maillist  -  Tu...@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Reply all
Reply to author
Forward
0 new messages