Python: Looking up country name from country code

2 Apr

A simple Python program to lookup the full country name (and other details) from country code. You can also look up timezones, capital, etc from this.

  1. Download this Gist from GitHub
  2. Save as countryinfo.py

After that, use this Python code in the same directory as countryinfo.py:

import countryinfo
countries = countryinfo.countries

countryCode = "US"

for country in countries:
  if country['code'] == countryCode:
    countryName = country['name']

print countryName

As you can see, the Gist has more than just the country name, so you could look up other info related to the country, such as timezone, capital, etc.

Advertisements

One Response to “Python: Looking up country name from country code”

  1. Dave October 17, 2012 at 6:36 pm #

    Thanks! This was exactly what I needed :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: