ISO国家、区域、语言、货币和脚本定义及其翻译
项目描述
pycountry 提供了标准的 ISO 数据库
该包包含从 Debian 的 pkg-isocodes 的副本,并通过 Python API 提供数据访问。
还包括各种字符串的翻译文件。
数据更新策略
pycountry 将不接受对数据的任何更改。这是一个围绕 Debian 的 pkg-isocodes 数据库(原样)的 ISO 标准的纯包装器。如果您需要更改世界政治状况,请与 ISO 或 Debian 的人联系,而不是与我联系。
捐赠/资金支持
这是一个我在个人时间维护的小项目。我对个人财务收益不感兴趣。然而,如果您想支持该项目,那么我很乐意如果您能向 Feminist Frequency 捐款。此外,也让世界知道您这样做,这样其他人也可以跟随您的脚步。
贡献
代码存储在 GitHub 上的 git 仓库 中,问题报告也必须在那里进行。
国家(ISO 3166-1)
国家可以通过一个数据库对象访问,该对象在导入 pycountry 时已经配置好,并作为一个可迭代对象工作
>>> import pycountry
>>> len(pycountry.countries)
249
>>> list(pycountry.countries)[0]
Country(alpha_2='AF', alpha_3='AFG', name='Afghanistan', numeric='004', official_name='Islamic Republic of Afghanistan')
可以通过各种代码查找特定的国家,并作为属性提供标准中包含的信息
>>> germany = pycountry.countries.get(alpha_2='DE')
>>> germany
Country(alpha_2='DE', alpha_3='DEU', name='Germany', numeric='276', official_name='Federal Republic of Germany')
>>> germany.alpha_2
'DE'
>>> germany.alpha_3
'DEU'
>>> germany.numeric
'276'
>>> germany.name
'Germany'
>>> germany.official_name
'Federal Republic of Germany'
还有一个“模糊”搜索,以帮助人们发现可能只是子区域的“正确”国家。模糊度也包括对 unicode 针对性进行标准化。还包括一些优先级,以优先匹配国家名称,并在具有更多匹配的国家之前列出具有较少匹配的国家
>>> pycountry.countries.search_fuzzy('England')
[Country(alpha_2='GB', alpha_3='GBR', name='United Kingdom', numeric='826', official_name='United Kingdom of Great Britain and Northern Ireland')]
>>> pycountry.countries.search_fuzzy('Cote')
[Country(alpha_2='CI', alpha_3='CIV', name="Côte d'Ivoire", numeric='384', official_name="Republic of Côte d'Ivoire"),
Country(alpha_2='FR', alpha_3='FRA', name='France', numeric='250', official_name='French Republic'),
Country(alpha_2='HN', alpha_3='HND', name='Honduras', numeric='340', official_name='Republic of Honduras')]
可以使用 __getattr__ 方法访问国家类的属性。如果请求的属性是国家类的键,则返回相应的值。在缺少“常用名称”或“官方名称”属性的特殊情况下,__getattr__ 将返回“名称”。以下是一些示例
>>> aland = pycountry.countries.get(alpha_2='AX')
>>> print(aland)
Country(alpha_2='AX', alpha_3='ALA', flag='🇦🇽', name='Åland Islands', numeric='248')
>>> aland.common_name
UserWarning: Country's common_name not found. Country name provided instead.
warnings.warn(warning_message, UserWarning)
'Åland Islands'
>>> aland.official_name
Country's official_name not found. Country name provided instead.
warnings.warn(warning_message, UserWarning)
'Åland Islands'
>>> aland.flag
'🇦🇽'
>>> aland.foo # Raises AttributeError
历史国家(ISO 3166-3)
historic_countries 数据库包含已被从标准中删除并现在包含在 ISO 3166-3 中的前国家,不包括现有的国家
>>> ussr = pycountry.historic_countries.get(alpha_3='SUN')
>>> ussr
Country(alpha_3='SUN', alpha_4='SUHH', withdrawal_date='1992-08-30', name='USSR, Union of Soviet Socialist Republics', numeric='810')
>>> ussr.alpha_4
'SUHH'
>>> ussr.alpha_3
'SUN'
>>> ussr.name
'USSR, Union of Soviet Socialist Republics'
>>> ussr.withdrawal_date
'1992-08-30'
国家子区域(ISO 3166-2)
国家子区域比国家本身更复杂,因为它们提供嵌套和类型化的结构。
可以直接访问所有子区域
>>> len(pycountry.subdivisions)
4847
>>> list(pycountry.subdivisions)[0]
Subdivision(code='AD-07', country_code='AD', name='Andorra la Vella', parent_code=None, type='Parish')
可以使用唯一的代码访问子区域。生成的对象将至少提供它们的代码、名称和类型
>>> de_st = pycountry.subdivisions.get(code='DE-ST')
>>> de_st.code
'DE-ST'
>>> de_st.name
'Sachsen-Anhalt'
>>> de_st.type
'State'
>>> de_st.country
Country(alpha_2='DE', alpha_3='DEU', name='Germany', numeric='276', official_name='Federal Republic of Germany')
一些子区域指定另一个子区域作为父级
>>> al_br = pycountry.subdivisions.get(code='AL-BU')
>>> al_br.code
'AL-BU'
>>> al_br.name
'Bulqiz\xeb'
>>> al_br.type
'District'
>>> al_br.parent_code
'AL-09'
>>> al_br.parent
Subdivision(code='AL-09', country_code='AL', name='Dib\xebr', parent_code=None, type='County')
>>> al_br.parent.name
'Dib\xebr'
可以使用国家代码索引查询单个国家的分区
>>> len(pycountry.subdivisions.get(country_code='DE'))
16
>>> len(pycountry.subdivisions.get(country_code='US'))
57
类似于国家,为子区域实现了 search_fuzzy 方法,以帮助发现相关的子区域条目。该方法包括对 unicode 针对性进行标准化,并优先匹配子区域名称。搜索算法旨在首先返回更相关的匹配项
此方法特别适用于不知道子区域的准确名称或代码的情况。
>>> pycountry.subdivisions.search_fuzzy('York')
[Subdivision(code='GB-YOR', country_code='GB', name='York', parent='GB-ENG', parent_code='GB-GB-ENG', type='Unitary authority')
Subdivision(code='GB-ERY', country_code='GB', name='East Riding of Yorkshire', parent='GB-ENG', parent_code='GB-GB-ENG', type='Unitary authority')
Subdivision(code='GB-NYK', country_code='GB', name='North Yorkshire', parent='GB-ENG', parent_code='GB-GB-ENG', type='Two-tier county')
Subdivision(code='US-NY', country_code='US', name='New York', parent_code=None, type='State')]
字符(ISO 15924)
字符可以从与国家类似的数据库中获取
>>> len(pycountry.scripts)
169
>>> list(pycountry.scripts)[0]
Script(alpha_4='Afak', name='Afaka', numeric='439')
>>> latin = pycountry.scripts.get(name='Latin')
>>> latin
Script(alpha_4='Latn', name='Latin', numeric='215')
>>> latin.alpha4
'Latn'
>>> latin.name
'Latin'
>>> latin.numeric
'215'
货币(ISO 4217)
货币数据库与之前的类似
>>> len(pycountry.currencies)
182
>>> list(pycountry.currencies)[0]
Currency(alpha_3='AED', name='UAE Dirham', numeric='784')
>>> argentine_peso = pycountry.currencies.get(alpha_3='ARS')
>>> argentine_peso
Currency(alpha_3='ARS', name='Argentine Peso', numeric='032')
>>> argentine_peso.alpha_3
'ARS'
>>> argentine_peso.name
'Argentine Peso'
>>> argentine_peso.numeric
'032'
语言(ISO 639-3)
语言数据库也是类似的
>>> len(pycountry.languages)
7874
>>> list(pycountry.languages)[0]
Language(alpha_3='aaa', name='Ghotuo', scope='I', type='L')
>>> aragonese = pycountry.languages.get(alpha_2='an')
>>> aragonese.alpha_2
'an'
>>> aragonese.alpha_3
'arg'
>>> aragonese.name
'Aragonese'
>>> bengali = pycountry.languages.get(alpha_2='bn')
>>> bengali.name
'Bengali'
>>> bengali.common_name
'Bangla'
区域设置
区域设置位于本包的 pycountry.LOCALES_DIR 子目录中。翻译域按照它们提供的翻译标准命名为 isoXXX。目录结构兼容 Python 的 gettext 模块。
以下是一个翻译语言名称的示例
>>> import gettext
>>> german = gettext.translation('iso3166-1', pycountry.LOCALES_DIR,
... languages=['de'])
>>> german.install()
>>> _('Germany')
'Deutschland'
查找
对于每个数据库(国家、语言、脚本等),您也可以在不了解可能匹配的键的情况下,进行不区分大小写的实体查找。例如
>>> pycountry.countries.lookup('de')
<pycountry.db.Country object at 0x...>
搜索以第一次匹配结束,并返回。
字典兼容性
您可以将其中的每个对象类型转换为 dict
>>> country = pycountry.countries.lookup('de')
>>> dict(country)
{'alpha_2': 'DE', 'name': 'Germany', ...}
自定义国家
虽然 pycountry 不会将其标准库添加非 ISO 值,但您可以根据需要动态添加或删除条目。
添加非 ISO 国家
>>> pycountry.countries.add_entry(alpha_2="XK", alpha_3="XXK", name="Kosovo", numeric="926")
从数据库中删除国家
>>> pycountry.countries.remove_entry(alpha_2="XK")
PyInstaller 兼容性
一些用户报告了在使用 PyCountry 与 PyInstaller 的问题。有关如何处理这些问题的指导,可以在 PyInstaller Google Group 中找到。
项目详情
下载文件
下载适合您平台文件的文件。如果您不确定要选择哪个,请了解更多关于 安装软件包 的信息。