我正在搜索包含国家/地区代码的国家/地区的完整列表。

类似于此页面(需要完整且有效):

http://www.nationsonline。 org / oneworld / country_code_list.htm

评论

Perl Locale :: Codes分发非常全面并得到积极维护。

如果您要查找的是开放数据,那么我想问的地方是开放数据栈交换。

#1 楼

ISO 3166-1官方网站可能是两个字母代码的最新来源。不幸的是,他们没有在线Alpha-3并引用其网站:

在哪里可以找到ISO 3316 / MA网站上免费下载的ISO 3166-1 alpha-3国家/地区代码?
无处。无法免费提供Alpha-3代码
。您可以从我们的ISO商店购买
国际标准ISO 3166-1
。它包含了三个字母的代码。
更新:
CIA网站上有一个列表,其中包括FIPS 10,ISO 3166 Alpha2,ISO 3166 Alpha3,STANAG和Internet TLD(例如.il或.uk)。 >链接摘要:

Alpha-2代码:ISO 3166-1官方网站

Alpha-3代码:Wikipedia或UN

这两种代码,加上STANAG和Internet TLD:CIA网站


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

评论


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

– Dirk van Bergen
2015年8月3日13:00

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

–亚当·马坦(Adam Matan)
15年8月3日在13:23

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

– Dirk van Bergen
15年8月3日,14:38

#2 楼

如果要定期更新列表,则可以抓取其中一个来源并将其结果解析为有用的格式。我这样做是为了将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
15年6月19日在13:22

嘿,这非常感谢-我在Mac上使用python3进行了即时更新,并做了一些更新:gist.github.com/j8jacobs/b4600638f34e7dfccfd007d667c2ec07

–朱利安
20年5月11日19:43

#3 楼

您可以在http://download.geonames.org/export/dump/countryInfo.txt中找到两个和三个字母代码中的全部(最多?)-它还具有ISO数字和Fips代码以及其他国家/地区信息。

评论


这是一种很好的格式,但是现在已经过时了。它不包含南苏丹和科索沃的代码。

–马修
13年2月22日在18:01

#4 楼

在许多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和外壳程序一个衬套的基本格式(例如,用于数据库导入):

$ 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 ,}'


或者,可以使用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
19 Mar 7 '19 at 9:45

#5 楼

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

ISO国家/地区,细分,语言,货币和脚本定义及其翻译
pycountry提供了该标准的ISO数据库:3166国家
3166-3已删除的国家
3166-2国家的细分
4217货币
15924脚本
该软件包包括Debian的pkg-isocodes的副本并制作数据可通过Python API访问。


#6 楼

您可以从http://datahub.io/dataset/iso-3166-1-alpha-2-country-codes/resource/9c3b30dd-f5f3-4bbe-a3cb获取完整的Alpha 2和Alpha 3列表(以及其他信息) -d7b2c21d66ce从http://datahub.io/dataset/iso-3166-1-alpha-2-country-codes有一个指向它的链接

#7 楼

我更新了@scw的脚本,该脚本从Wikipedia抓取了数据。现在,它使用requests而不是Beautiful Soup 4的urllib2,并且它输出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密钥更改为“ alpha_3”或“ alpha_2”而不是列表?

– Benck
16年4月4日在7:20

#8 楼

您可以使用以下代码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

#9 楼

Open Knowledge Foundation上也有一个很棒的数据集,其中包括ISO 3166 alpha3,alpha2,数字以及许多其他数据。

http://data.okfn.org/data/core/country-codes #data

https://github.com/datasets/国家/地区代码

#10 楼

我在github仓库上找到了一个非常不错的数据库-https://github.com/stefangabos/world_countries

在撰写本文时,存储库由jsoncsvsql文件组成,适用于22种语言,具有不同的国家/地区代码:ISO 3166-1 alpha-3,ISO 3166-1 alpha-2和全名。

数据库似乎定期更新

#11 楼

更新了urllib3和bs4的原始答案之一

# https://gis.stackexchange.com/questions/1047/seeking-full-list-of-iso-alpha-2-and-iso-alpha-3-country-codes
# If you want to periodically update your list, you could scrape one of the sources and parse its results into a
# useful format. I've done so here for converting the Wikipedia country code list into a CSV:

import csv
import urllib3
from bs4 import BeautifulSoup

url = 'http://en.wikipedia.org/wiki/ISO_3166-1'
http = urllib3.PoolManager()
response = http.request('GET', url)
print(f"responce: {response.status}")
print(f"received: {len(response.data)} bytes")

soup = BeautifulSoup(response.data, features="html.parser")

# "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('ripped_wikipedia-iso-country-codes.csv', 'w'), quoting=csv.QUOTE_MINIMAL)

# get the header rows, write to the CSV
iso_csv.writerow([th.findAll(text=True)[0].strip() 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]
    # first take the name
    cols = [raw_cols[0][1]]
    # for all other columns, use the first result text, and loose the lines shifts
    cols.extend([col[0].strip() for col in raw_cols[1:]])
    iso_csv.writerow(cols)


#12 楼




从世界银行获取API。
他们在XMLJSON中都提供了响应
有关更多信息,请参阅其开发人员页面:https://datahelpdesk.worldbank.org/knowledgebase/articles/898590 -country-api-queries

#13 楼

这是一个旧线程,但值得为此进行更新。
在Alpha2和Alpha3国家/地区代码中进行正向/反向查询,会在每个国家/地区返回一个庞大的对象,其中包括电话代码,货币,ISO,IOC信息,邮政编码,以及更多:
https://github.com/rolinger/iso-country-data-validation

#14 楼

尝试以下列表:

https://gist.github.com/eparreno/205900

它具有ISO 2字母,3字母和带有国家简称的数字代码名称。

#15 楼

一个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");


#16 楼

如果您不想对国家/地区列表进行硬编码(我不建议这样做,因为它会发生很大变化),请使用此URL,从中获取JSON格式的2字母代码和国家/地区名称:
annsystem .com / api / getCountry

还包括联合国和非联合国成员国。

有关详细信息和参数,请参见:flossk.org/en/blog/country-列出全部良好

#17 楼

如果有任何R用户偶然发现此线程,则可以使用以下R解决方案:来自包装文档:


支持以下编码方案:战争字符相关,
连续数字,ISO3字符,ISO3字符,ISO2字符,IMF
数字,国际奥委会,FIPS 10-4,粮农组织数字,
联合国数字,世界银行字符,官方英文简称
国名(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")


#18 楼

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