跳转到主要内容

不同ISO 3166国家代码之间转换的便捷API。

项目描述

incf.countryutils 提供了一个在根据大陆(data_file)列出的国家列表之上的便捷API,如维基百科上找到的(副本包含在发行版中)。

它支持不同类型的国家代码和名称之间的转换,例如

>>> from incf.countryutils import transformations
>>> transformations.cca2_to_ccn('de')
'276'
>>> transformations.ccn_to_cn(276)
'Germany'

支持的国家代码包括:数字ccn;内部引用格式)、两字母国家代码cca2)和三字母国家代码cca3)。此外,incf.countryutils 知道每个国家的简单英语名称cn)以及官方英语名称

转换名称遵循模式 <f1>_to_<f2>,其中 f1f2 指的是国家代码或名称。所有到和从参考格式(ccn)的转换都提供了。

当提供一个数值国家代码作为参数整数以及字符串表示形式都被接受。

此外,还有一些组合转换可供方便使用。最值得注意的是这是 <任何国家代码>_to_<简单或官方名称>

>>> transformations.cc_to_cn('FR')
'France'
>>> transformations.cc_to_cn('FRa')
'France'
>>> transformations.cc_to_cn('250')
'France'
>>> transformations.cc_to_cn(250)
'France'
>>> transformations.cc_to_con(250)
'French Republic France'

按名称查找仅支持简单英语名称

>>> transformations.cn_to_ccn('Italy')
'380'
>>> transformations.ccn_to_con('380')
'Italian Republic Italy'

大陆

基于来自维基百科的数据,incf.countryutils 允许查找一个国家所属的大洲(ctn:大洲名称;ctca2:两字母大洲代码)。

>>> transformations.cn_to_ctn('Italy')
'Europe'
>>> transformations.cca_to_ctn('us')
'North America'

大洲也有两字母代码,例如:

>>> transformations.cca_to_ctca2('usa')
'NA'

给定一个大洲,可以获取其构成国家。

>>> transformations.ctca2_to_ccn('AN')
['010', '074', '260', '334', '239']

替代API(OO)

存在一个基于《国家》和《大洲》类型概念的、更为面向对象的替代API。在创建时,国家或大洲需要传递一个名称或代码。

>>> from incf.countryutils.datatypes import Country
>>> china = Country('China')
>>> china
<incf.countryutils.datatypes.Country object at 0x...>

国家的名称和代码作为属性可用

>>> china.name
'China'
>>> china.official_name
"People's Republic of China"
>>> china.numeric
'156'
>>> china.alpha2
'CN'
>>> china.alpha3
'CHN'

并且 continent 属性指向相应的 Continent 实例

>>> china.continent
<incf.countryutils.datatypes.Continent object at 0x...>

该实例具有以下属性:

>>> china.continent.name
'Asia'
>>> china.continent.alpha2
'AS'

询问大洲其构成国家将返回一个生成器对象,依次返回国家实例

>>> china.continent.countries
<generator object at 0x...>
>>> china.continent.countries.next()
<incf.countryutils.datatypes.Country object at 0x...>
>>> [c.name for c in china.continent.countries]
['Afghanistan', 'Armenia', 'Azerbaijan', 'Bahrain', ...]

变更日志

1.0 - (2009-04-20)

  • 初始发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码发行版

incf.countryutils-1.0.tar.gz (21.6 kB 查看散列)

上传时间 源代码

支持者