Mencari daftar lengkap kode negara ISO ALPHA-2 dan ISO ALPHA-3?


Jawaban:


31

Situs resmi ISO 3166-1 mungkin merupakan sumber terbaru untuk kode dua huruf. Sayangnya, mereka tidak memiliki alpha-3 online, mengutip situs mereka :

Di mana saya dapat menemukan kode negara ISO 3166-1 alpha-3 untuk diunduh gratis di Situs Web ISO 3166 / MA?

Tidak ada tempat Kode alpha-3 tidak tersedia secara gratis. Anda dapat membeli Standar Internasional ISO 3166-1 dari Toko ISO kami. Ini berisi kode tiga huruf.

Agak aneh di era internet, tapi untungnya, ada artikel Wikipedia dengan daftar lengkap dan dokumen resmi PBB yang membahas masalah ini, dengan kode negara .

Memperbarui:

Ada daftar di situs CIA dengan FIPS 10, ISO 3166 Alpha2, ISO 3166 Alpha3, STANAG dan TLD Internet (misalnya, .il atau .uk).

Ringkasan tautan :

Perhatikan bahwa daftar ini berisi entitas non-negara seperti Antartika.


2
Jawaban ini sekarang 100% bermanfaat sebagai tautan pertama, situs resmi, sekarang juga menyediakan kode alpha-3 gratis di tabel copypastable yang diformat dengan baik.
Dirk van Bergen

@ DirkvanBergen Itu langkah yang bagus, tapi meja mereka jauh dari yang diformat dengan baik. Pagination 10 halaman, tanpa tautan CSV.
Adam Matan

4
Gunakan tautan di sebelah kiri untuk hanya menampilkan kode yang ditetapkan dan kemudian atur halaman untuk menampilkan 300 dan Anda memiliki semuanya, salin tempel ke excel dan (ab) gunakan dengan cara apa pun yang Anda inginkan.
Dirk van Bergen

14

Jika Anda ingin memperbarui daftar secara berkala, Anda dapat mengikis salah satu sumber dan memilah hasilnya ke dalam format yang bermanfaat. Saya sudah melakukannya di sini untuk mengubah daftar kode negara Wikipedia menjadi 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)

Saya suka pendekatan ini. Saya memperbarui skrip ini untuk menggunakan pustaka yang lebih baru dan menghasilkan JSON alih-alih menulis ke file CSV: gis.stackexchange.com/a/151571/54020
gitaarik


6

Pada banyak distribusi Linux, daftar kode negara iso diinstal secara default di bawah:

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

Di bawah Fedora / CentOS / RHEL / Debian , paket yang berisi file ini disebut iso-codes( beranda proyek ).

File XML berisi pemetaan dalam struktur hierarkis:

<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" />
[..]

Itu dapat diubah menjadi format berbasis catatan (misalnya untuk impor basis data) melalui XPath dan shell one liner:

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

Atau, satu dapat menggunakan Python modul pycountry untuk membaca dan mengubah kode dari paket yang, misalnya:

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

Dari kode sumber debian salsa.debian.org/iso-codes-team/iso-codes yang juga mencakup terjemahan .po
polesen

5

Saya ingin menambahkan pycountry karena Anda memiliki tag python dan sepertinya itulah yang Anda inginkan. Dari dokumen:

Negara ISO, subdivisi, bahasa, mata uang dan definisi skrip dan terjemahannya

pycountry menyediakan basis data ISO untuk standar:

639 Bahasa

3166 Negara

3166-3 Negara yang dihapus

3166-2 Pembagian negara

4217 Mata Uang

15924 Script

Paket termasuk salinan dari pkg-isocodes Debian dan membuat data dapat diakses melalui API Python.



2

Saya memperbarui skrip @ scw yang menghapus data dari Wikipedia. Sekarang ia menggunakan requestsbukan urllib2, Beautiful Soup 4 dan menghasilkan JSON alih-alih menulis ke file 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
))

Output JSON seperti:

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

    ...

Saya suka metode ini, apakah mungkin mengubah kunci JSON menjadi "alpha_3" atau "alpha_2" sebagai ganti daftar?
Benck

1

Anda dapat menggunakan kode ini https://classic.scraperwiki.com/scrapers/iso_3166-1/edit/ - lxml selalu lebih cepat daripada BeautifulSoup.

Salin di sini:

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")

Satu lagi perpustakaan yang bagus: https://github.com/neuront/python-iso3166



1

Saya menemukan database yang sangat bagus di repo github - https://github.com/stefangabos/world_countries

Pada saat menulis repositori terdiri dari json, csv, sqlfile untuk 22 bahasa dengan kode negara yang berbeda: ISO 3166-1 alpha-3, ISO 3166-1 alpha-2 dan nama lengkap.

Database tampaknya diperbarui secara teratur



0

php Array dengan 3 huruf kode negara ISO dari artikel Wikipedia

Saya menyalin dan menempel daftar dari Wikipedia dan membuat array. Mungkin kode ini dapat membantu seseorang menghemat waktu, yang ingin membuat berbagai kode negara. Saya tidak terbiasa dengan python, tetapi pembuatan array harus mirip dengan 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

Jika ada pengguna R yang menemukan thread ini, inilah solusi R:

The countrycodepaket berisi daftar lengkap kode negara dalam berbagai format. Dari dokumentasi paket:

Mendukung skema pengkodean berikut: Korelasi karakter Perang, CoW-numeric, ISO3-numeric, ISO3-numeric, ISO2-numeric, numeric IMF, Komite Olimpiade Internasional, FIPS 10-4, FAO numeric, numerik PBB, numerik Bank Dunia, nama negara pendek resmi bahasa Inggris (ISO), benua, wilayah.

Paket ini juga akan mengkonversi antara kode yang berbeda dan dapat mengidentifikasi negara dengan nama standar atau non-standar menggunakan ekspresi reguler.

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

Cukup Gunakan alat Microsoft Excel Power BI untuk mengekstrak data dari Wikipedia. Butuh waktu kurang dari 30 detik untuk membuat goresan excel halaman dan kemudian menyimpannya ke format apa pun yang Anda suka.



Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.