[Tutor] PyCountry currency formatting woes

Sithembewena Lloyd Dube zebra05 at gmail.com
Sun May 4 22:55:50 CEST 2014


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?


On Sun, May 4, 2014 at 10:25 PM, Sithembewena Lloyd Dube
<zebra05 at gmail.com>wrote:

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



-- 
Regards,
Sithu Lloyd Dube
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140504/8e8116e0/attachment-0001.html>


More information about the Tutor mailing list