寻求ISO ALPHA-2和ISO ALPHA-3国家/地区代码的完整列表?


Answers:


31

ISO 3166-1官方网站很可能是两个字母代码的最更新的源。不幸的是,他们没有在线alpha-3,引用了他们的网站

在哪里可以在ISO 3166 / MA网站上免费下载ISO 3166-1 alpha-3国家/地区代码?

无处。并非免费提供alpha-3代码。您可以从我们的ISO商店购买国际标准ISO 3166-1。它包含三个字母的代码。

在互联网时代有点奇怪,但是幸运的是,有一篇Wikipedia文章,其中包含完整列表,以及一份涵盖该主题联合国官方文档,其中包含国家/地区代码

更新:

在CIA网站有一个列表,列出了FIPS 10,ISO 3166 Alpha2,ISO 3166 Alpha3,STANAG和Internet TLD(例如.il或.uk)。

链接摘要

请注意,这些列表包含非国家实体,例如Antartica。


2
现在,此答案作为第一个链接(即官方网站)具有100%的用处,现在还可以在格式良好的copypastable表中免费提供alpha-3代码。
Dirk van Bergen 2015年

@DirkvanBergen这是一个不错的举动,但是它们的表远没有被很好地格式化。10页分页,没有CSV链接。
亚当·马坦

4
使用左侧的链接仅显示分配的代码,然后将页面设置为显示300,您已全部拥有,将粘贴内容复制到excel,然后以您希望的任何方式使用它。
Dirk van Bergen

14

如果要定期更新列表,则可以抓取其中一个来源并将其结果解析为有用的格式。我这样做是为了将Wikipedia 国家/地区代码列表转换为CSV

import csv
import urllib2
from BeautifulSoup import BeautifulSoup

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

url = 'http://en.wikipedia.org/wiki/ISO_3166-1'

page = opener.open(url)
soup = BeautifulSoup(page.read())

# "Current Codes" is second table on the page
t = soup.findAll('table', {'class' : 'wikitable sortable'})[1]

# create a new CSV for the output
iso_csv = csv.writer(open('wikipedia-iso-country-codes.csv', 'w'))

# get the header rows, write to the CSV
iso_csv.writerow([th.findAll(text=True)[0] for th in t.findAll('th')])

# Iterate over the table pulling out the country table results. Skip the first 
# row as it contains the already-parsed header information.
for row in t.findAll("tr")[1:]:
    tds = row.findAll('td')
    raw_cols = [td.findAll(text=True) for td in tds]
    cols = []
    # country field contains differing numbers of elements, due to the flag -- 
    # only take the name
    cols.append(raw_cols[0][-1:][0])
    # for all other columns, use the first result text
    cols.extend([col[0] for col in raw_cols[1:]])
    iso_csv.writerow(cols)

我喜欢这种方法。我更新了此脚本,以使用更多最新的库并输出JSON而不是写入CSV文件:gis.stackexchange.com/a/151571/54020
gitaarik 2015年


6

在许多Linux发行版中,默认情况下在以下位置安装了iso国家(地区)代码列表:

/usr/share/xml/iso-codes/iso_3166.xml

Fedora / CentOS / RHEL / Debian下,包含此文件的软件包称为iso-codes项目主页)。

XML文件在层次结构中包含映射:

<iso_3166_entries>
    <iso_3166_entry
            alpha_2_code="AF"
            alpha_3_code="AFG"
            numeric_code="004"
            name="Afghanistan"
            official_name="Islamic Republic of Afghanistan" />
[..]

它可以通过XPath和一个shell shell转换为基于记录的格式(例如,用于数据库导入):

$ xmllint --noout --xpath \
     '//iso_3166_entry/@*[name() = "alpha_2_code" or name()="alpha_3_code"]' \
     /usr/share/xml/iso-codes/iso_3166.xml \
    | sed 's/alpha_2/\nalpha_2/g' \
    | awk -F'"' 'OFS="," {print $2,$4}'

或者,可以使用Python模块pycountry从该包中读取和转换代码,例如:

$ pip3 install --user pycountry
$ python3
>>> import pycountry
>>> for i in pycountry.countries:
...   print('{},{}'.format(i.alpha2,i.alpha3))
...
AF,AFG
AX,ALA
AL,ALB
[..]

来自debian源代码salsa.debian.org/iso-codes-team/iso-codes,其中也包括.po翻译
polesen

5

我想添加pycountry,因为您有一个python标签,而这似乎正是您想要的。从文档:

ISO国家/地区,细分,语言,货币和脚本定义及其翻译

pycountry为这些标准提供了ISO数据库:

639种语言

3166个国家

3166-3已删除的国家

3166-2国家的细分

4217货币

15924脚本

该软件包包括Debian的pkg-isocodes的副本,并使数据可以通过Python API访问。



2

我更新了@scw的脚本,该脚本从Wikipedia抓取了数据。现在,它使用requests而不是urllib2Beautiful Soup 4,并且它输出JSON而不是写入CSV文件。

import json
import bs4
import requests

print(json.dumps(
    [
        {
            ['name', 'alpha_2', 'alpha_3', 'numeric'][no]:
            td.find_all()[-1].text
            for no, td in enumerate(row.find_all('td')[:-1])
        }
        for row in bs4.BeautifulSoup(
            requests.get('http://en.wikipedia.org/wiki/ISO_3166-1').text
        ).find('table', {'class': 'wikitable sortable'}).find_all('tr')[1:]
    ],
    indent=4,
    ensure_ascii=False
))

输出如下的JSON:

[
    {
        "name": "Afghanistan",
        "alpha_3": "AFG",
        "alpha_2": "AF",
        "numeric": "004"
    },
    {
        "name": "Åland Islands",
        "alpha_3": "ALA",
        "alpha_2": "AX",
        "numeric": "248"
    },

    ...

我喜欢这种方法,是否可以将JSON键更改为“ alpha_3”或“ alpha_2”而不是列表?
benck '16

1

您可以使用以下代码https://classic.scraperwiki.com/scrapers/iso_3166-1/edit/-lxml总是比BeautifulSoup快。

在这里复制:

import scraperwiki
import lxml.html
import urllib
import datetime
import json

from unidecode import unidecode

def get_html(title):
    raw_json = scraperwiki.scrape("http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + title)
    html = json.loads(raw_json)['parse']['text']['*']
    return html

page_title = "ISO_3166-1"

html = get_html(page_title)
doc = lxml.html.fromstring(html)

for count, tr in enumerate(doc.cssselect('tr')):
    row = [(td.text_content()) for td in tr.cssselect('td')]
    if len(row)==5:
        for ahref in tr.cssselect('a'):
            detailink = ahref.attrib['href']
            if detailink.find(':',0,len(detailink)) != -1:
                detailink = detailink[6:]
                print detailink
        now = datetime.datetime.now()
        data ={"tmsp_scraped":str(now), "eng_short_name":row[0], "alpha_2_code":row[1], "alpha_3_code":row[2], "numeric_code":row[3], "iso_31662_code":detailink}
        scraperwiki.sqlite.save(unique_keys=["eng_short_name"], data=data, table_name="s_iso31661")

        html = get_html(detailink)
        doc = lxml.html.fromstring(html)

        for count, tr in enumerate(doc.cssselect('tr')):
            row = [td.text_content() for td in tr.cssselect('td')]
            row2 = [td.text_content() for td in tr.cssselect('td')]
            if len(row)>0:
                if row[0][:2] == detailink[11:]:
                    now = datetime.datetime.now()
                    data = {"tmsp_scraped":str(now), "iso_31662_code":detailink, "region_code":row[0], "region_desc":row[1], "region_desc_utf8":row2[1]}
                    scraperwiki.sqlite.save(unique_keys=["iso_31662_code","region_code"], data=data, table_name="s_iso31662_region")

一个更好的库:https : //github.com/neuront/python-iso3166



1

我在github仓库上找到了一个很好的数据库-https: //github.com/stefangabos/world_countries

在编写存储库的时刻包括jsoncsvsql22种语言,不同国家代码的文件:ISO 3166-1阿尔法-3,ISO 3166-1的α-2和全名。

数据库似乎定期更新



0

带有Wikipedia文章的 3个字母的ISO国家/地区代码的php数组

我从Wikipedia复制并粘贴了列表,并创建了数组。也许这段代码可以帮助某人节省一些时间,但要创建一组国家代码。我不熟悉python,但数组创建应类似于php。

$Countries=array();

array_push($Countries,"ABW");
array_push($Countries,"AFG");
array_push($Countries,"AGO");
array_push($Countries,"AIA");
array_push($Countries,"ALA");
array_push($Countries,"ALB");
array_push($Countries,"AND");
array_push($Countries,"ARE");
array_push($Countries,"ARG");
array_push($Countries,"ARM");
array_push($Countries,"ASM");
array_push($Countries,"ATA");
array_push($Countries,"ATF");
array_push($Countries,"ATG");
array_push($Countries,"AUS");
array_push($Countries,"AUT");
array_push($Countries,"AZE");
array_push($Countries,"BDI");
array_push($Countries,"BEL");
array_push($Countries,"BEN");
array_push($Countries,"BES");
array_push($Countries,"BFA");
array_push($Countries,"BGD");
array_push($Countries,"BGR");
array_push($Countries,"BHR");
array_push($Countries,"BHS");
array_push($Countries,"BIH");
array_push($Countries,"BLM");
array_push($Countries,"BLR");
array_push($Countries,"BLZ");
array_push($Countries,"BMU");
array_push($Countries,"BOL");
array_push($Countries,"BRA");
array_push($Countries,"BRB");
array_push($Countries,"BRN");
array_push($Countries,"BTN");
array_push($Countries,"BVT");
array_push($Countries,"BWA");
array_push($Countries,"CAF");
array_push($Countries,"CAN");
array_push($Countries,"CCK");
array_push($Countries,"CHE");
array_push($Countries,"CHL");
array_push($Countries,"CHN");
array_push($Countries,"CIV");
array_push($Countries,"CMR");
array_push($Countries,"COD");
array_push($Countries,"COG");
array_push($Countries,"COK");
array_push($Countries,"COL");
array_push($Countries,"COM");
array_push($Countries,"CPV");
array_push($Countries,"CRI");
array_push($Countries,"CUB");
array_push($Countries,"CUW");
array_push($Countries,"CXR");
array_push($Countries,"CYM");
array_push($Countries,"CYP");
array_push($Countries,"CZE");
array_push($Countries,"DEU");
array_push($Countries,"DJI");
array_push($Countries,"DMA");
array_push($Countries,"DNK");
array_push($Countries,"DOM");
array_push($Countries,"DZA");
array_push($Countries,"ECU");
array_push($Countries,"EGY");
array_push($Countries,"ERI");
array_push($Countries,"ESH");
array_push($Countries,"ESP");
array_push($Countries,"EST");
array_push($Countries,"ETH");
array_push($Countries,"FIN");
array_push($Countries,"FJI");
array_push($Countries,"FLK");
array_push($Countries,"FRA");
array_push($Countries,"FRO");
array_push($Countries,"FSM");
array_push($Countries,"GAB");
array_push($Countries,"GBR");
array_push($Countries,"GEO");
array_push($Countries,"GGY");
array_push($Countries,"GHA");
array_push($Countries,"GIB");
array_push($Countries,"GIN");
array_push($Countries,"GLP");
array_push($Countries,"GMB");
array_push($Countries,"GNB");
array_push($Countries,"GNQ");
array_push($Countries,"GRC");
array_push($Countries,"GRD");
array_push($Countries,"GRL");
array_push($Countries,"GTM");
array_push($Countries,"GUF");
array_push($Countries,"GUM");
array_push($Countries,"GUY");
array_push($Countries,"HKG");
array_push($Countries,"HMD");
array_push($Countries,"HND");
array_push($Countries,"HRV");
array_push($Countries,"HTI");
array_push($Countries,"HUN");
array_push($Countries,"IDN");
array_push($Countries,"IMN");
array_push($Countries,"IND");
array_push($Countries,"IOT");
array_push($Countries,"IRL");
array_push($Countries,"IRN");
array_push($Countries,"IRQ");
array_push($Countries,"ISL");
array_push($Countries,"ISR");
array_push($Countries,"ITA");
array_push($Countries,"JAM");
array_push($Countries,"JEY");
array_push($Countries,"JOR");
array_push($Countries,"JPN");
array_push($Countries,"KAZ");
array_push($Countries,"KEN");
array_push($Countries,"KGZ");
array_push($Countries,"KHM");
array_push($Countries,"KIR");
array_push($Countries,"KNA");
array_push($Countries,"KOR");
array_push($Countries,"KWT");
array_push($Countries,"LAO");
array_push($Countries,"LBN");
array_push($Countries,"LBR");
array_push($Countries,"LBY");
array_push($Countries,"LCA");
array_push($Countries,"LIE");
array_push($Countries,"LKA");
array_push($Countries,"LSO");
array_push($Countries,"LTU");
array_push($Countries,"LUX");
array_push($Countries,"LVA");
array_push($Countries,"MAC");
array_push($Countries,"MAF");
array_push($Countries,"MAR");
array_push($Countries,"MCO");
array_push($Countries,"MDA");
array_push($Countries,"MDG");
array_push($Countries,"MDV");
array_push($Countries,"MEX");
array_push($Countries,"MHL");
array_push($Countries,"MKD");
array_push($Countries,"MLI");
array_push($Countries,"MLT");
array_push($Countries,"MMR");
array_push($Countries,"MNE");
array_push($Countries,"MNG");
array_push($Countries,"MNP");
array_push($Countries,"MOZ");
array_push($Countries,"MRT");
array_push($Countries,"MSR");
array_push($Countries,"MTQ");
array_push($Countries,"MUS");
array_push($Countries,"MWI");
array_push($Countries,"MYS");
array_push($Countries,"MYT");
array_push($Countries,"NAM");
array_push($Countries,"NCL");
array_push($Countries,"NER");
array_push($Countries,"NFK");
array_push($Countries,"NGA");
array_push($Countries,"NIC");
array_push($Countries,"NIU");
array_push($Countries,"NLD");
array_push($Countries,"NOR");
array_push($Countries,"NPL");
array_push($Countries,"NRU");
array_push($Countries,"NZL");
array_push($Countries,"OMN");
array_push($Countries,"PAK");
array_push($Countries,"PAN");
array_push($Countries,"PCN");
array_push($Countries,"PER");
array_push($Countries,"PHL");
array_push($Countries,"PLW");
array_push($Countries,"PNG");
array_push($Countries,"POL");
array_push($Countries,"PRI");
array_push($Countries,"PRK");
array_push($Countries,"PRT");
array_push($Countries,"PRY");
array_push($Countries,"PSE");
array_push($Countries,"PYF");
array_push($Countries,"QAT");
array_push($Countries,"REU");
array_push($Countries,"ROU");
array_push($Countries,"RUS");
array_push($Countries,"RWA");
array_push($Countries,"SAU");
array_push($Countries,"SDN");
array_push($Countries,"SEN");
array_push($Countries,"SGP");
array_push($Countries,"SGS");
array_push($Countries,"SHN");
array_push($Countries,"SJM");
array_push($Countries,"SLB");
array_push($Countries,"SLE");
array_push($Countries,"SLV");
array_push($Countries,"SMR");
array_push($Countries,"SOM");
array_push($Countries,"SPM");
array_push($Countries,"SRB");
array_push($Countries,"SSD");
array_push($Countries,"STP");
array_push($Countries,"SUR");
array_push($Countries,"SVK");
array_push($Countries,"SVN");
array_push($Countries,"SWE");
array_push($Countries,"SWZ");
array_push($Countries,"SXM");
array_push($Countries,"SYC");
array_push($Countries,"SYR");
array_push($Countries,"TCA");
array_push($Countries,"TCD");
array_push($Countries,"TGO");
array_push($Countries,"THA");
array_push($Countries,"TJK");
array_push($Countries,"TKL");
array_push($Countries,"TKM");
array_push($Countries,"TLS");
array_push($Countries,"TON");
array_push($Countries,"TTO");
array_push($Countries,"TUN");
array_push($Countries,"TUR");
array_push($Countries,"TUV");
array_push($Countries,"TWN");
array_push($Countries,"TZA");
array_push($Countries,"UGA");
array_push($Countries,"UKR");
array_push($Countries,"UMI");
array_push($Countries,"URY");
array_push($Countries,"USA");
array_push($Countries,"UZB");
array_push($Countries,"VAT");
array_push($Countries,"VCT");
array_push($Countries,"VEN");
array_push($Countries,"VGB");
array_push($Countries,"VIR");
array_push($Countries,"VNM");
array_push($Countries,"VUT");
array_push($Countries,"WLF");
array_push($Countries,"WSM");
array_push($Countries,"YEM");
array_push($Countries,"ZAF");
array_push($Countries,"ZMB");
array_push($Countries,"ZWE");


0

如果有任何R用户偶然发现此线程,这是R解决方案:

countrycode软件包包含许多不同格式的国家代码的完整列表。从包装文件中:

支持以下编码方案:战争字符,CoW数字,ISO3字符,ISO3数字,ISO2字符,IMF数字,国际奥委会,FIPS 10-4,FAO数字,联合国数字,世界银行字符,官方英文缩写国家名称(ISO),大陆,地区。

该软件包还将在不同的代码之间转换,并可以使用正则表达式按标准或非标准名称来标识国家。

library(countrycode)
# data frame of country names and codes
head(countrycode_data)
# convert from CoW to ISO3
countrycode(c("ALG","ALB","UKG","CAN","USA"), origin = "cowc", destination = "iso3c")
# ISO2 code from non-standard name
countrycode("Britain", "country.name", "iso2c")

0

只需使用Microsoft Excel Power BI工具即可从Wikipedia中提取数据。创建页面的Excel抓取内容不到30秒,然后将其保存为所需的任何格式。



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.