gpt4 book ai didi

python - 'DE' alpha2 国家代码的 PyCountry 货币格式问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:42 24 4
gpt4 key购买 nike

我有一个 Python 函数,它接受一个 alpha2 国家代码和一个价格字符串,其目的是获取该国家/地区的货币并使用该货币的 currency.letter 属性使用字符串插值来格式化提供的价格字符串。

上面的代码到目前为止工作正常 - 但当以德国作为国家调用时它会失败,如下所示:

>>> 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(德国的数字)的货币 - 但它确实包含欧元。关于解决这个问题的方法有什么想法吗?

最佳答案

不幸的是,国家数字代码与货币数字不同。根据 ISO,“Where possible the 3 digit numeric code is the same as the numeric country code”- 但这对于由多个国家共享的欧元来说显然是不可能的。

欧元的数字是 978,而不是 276;显然 pycountry 不提供国家数字和货币数字之间的映射。这是原始表格的链接(XML 或 XLS 格式),因此如果您愿意,您可以自己制作... http://www.currency-iso.org/en/home/tables/table-a1.html

关于python - 'DE' alpha2 国家代码的 PyCountry 货币格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23461352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com