PyCountry货币格式问题为"DE" alpha2国家/地区代码 [英] PyCountry currency formatting woes for 'DE' alpha2 country code
问题描述
我有一个Python函数,该函数接受一个alpha2国家代码和一个价格字符串,其目的是获取该国家的货币并使用该货币的currency.letter属性来通过字符串插值来格式化所提供的价格字符串.
I have a Python 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 using string interpolation.
上述方法到目前为止效果很好-但是在以德国为国家的情况下,它却落空了:
The above works fine so far - yet it falls over when called with Germany as the country as follows:
>>> import pycountry
>>> country = pycountry.countries.get(alpha2='DE')
>>> currency = pycountry.currencies.get(numeric=country.numeric)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
return self.indices[field][value]
KeyError: '276'
>>>
pycountry.countries
集合不包含数字为276(德国的数字)的货币-但确实包含欧元.关于如何解决这个问题有什么想法吗?
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?
推荐答案
不幸的是,国家/地区数字代码与货币数字不同.根据ISO的规定,"在可能的情况下,三位数的数字代码应与数字的国家/地区代码相同 -但这显然是不可能的,欧元由多个国家/地区共享.
Unfortunately, the country numeric code is NOT the same as the currency numeric. According to the ISO, "Where possible the 3 digit numeric code is the same as the numeric country code" - but this is obviously NOT possible for the Euro, which is shared by multiple countries.
欧元的数字是978,而不是276;显然pycountry没有提供国家/地区数字和货币数字之间的映射.这是原始表(XML或XLS格式)的链接,因此您可以根据需要滚动自己的表... http://www.currency-iso.org/en/home/tables/table-a1.html
The numeric for the Euro is 978, not 276; apparently pycountry doesn't provide a mapping between country numerics and currency numerics. Here's a link to the raw tables (in XML or XLS format) so you can roll your own, if you so desire... http://www.currency-iso.org/en/home/tables/table-a1.html
这篇关于PyCountry货币格式问题为"DE" alpha2国家/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!