Seeking full list of ISO ALPHA-2 and ISO ALPHA-3 country codes?
The ISO 3166-1 official site is probably the most updated source for the two-letter codes. Unfortunately, they don't have the alpha-3 online, quoting their site:
Where can I find the ISO 3166-1 alpha-3 country code for free download on the ISO 3166/MA Website?
Nowhere. The alpha-3 code is not made available free of charge. You can buy the International Standard ISO 3166-1 from our ISO Store. It contains the three-letter code.
A bit strange in the internet era, but luckily, there is a Wikipedia article with the full list and a UN official document that covers the subject, with country codes.
Update:
There's a list at the CIA site with FIPS 10, ISO 3166 Alpha2, ISO 3166 Alpha3, STANAG and Internet TLD (e.g, .il or .uk).
Link summary:
- Alpha-2 codes: ISO 3166-1 official site
- Alpha-3 codes: Wikipedia or UN
- Both codes, plus STANAG and Internet TLD: CIA site
Note that these list contain non-country entities like Antartica.
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 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)
You can find all (most?) of the two and three letter codes in http://download.geonames.org/export/dump/countryInfo.txt - it also has ISO numeric and fips codes and other country info.