2
votes

I am trying to create a Python script to read through a column in a CSV file that will read a Country Name, but then it will change that name to a Country Code using the ISO 3166 Alpha2 standard.

I have referenced pycountries: Convert Country Names (Possibly Incomplete!) to CountryCodes, but I'm having difficulty with the code.

Basically, I am taking a CSV file that has hundreds to thousands of entries in a column, but I need to convert the full name to a country code based on the ISO 3166 standard. I have been playing around with "pycountry" and the various functions in the python csv module, but I haven't been able to get it to work. I then want to overwrite the country name with the country code or at the very least output to a new file. Suggestions?

I am using something like the following for the rest of my code, but I'm not entirely sure how to write this country conversion I want to do:

import sys
import csv
import pycountry

csv_file = csv.DictReader(open(sys.argv[1], 'rb'))

for column in csv_file:
    X = column['name']...
1
I wrote a command line program that can convert countries stored in several formats to/from a wide variety of formats; though it's not Python: github.com/sshaw/normalize_country#conversion-utility - sshaw

1 Answers

2
votes

Iterate over pycountry.countries and initialize a mapping name -> short name (alpha2, or alpha3):

mapping = {country.name: country.alpha2 for country in pycountry.countries}
for column in csv_file:
    print column['name'], mapping.get(column['name'], 'No country found')

For the file containing:

name
Kazakhstan
Ukraine

it prints:

Kazakhstan KZ
Ukraine UA