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