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']...