csv - Convert Country Names to Country Code using Python DictReader/DictWriter -


i trying create python script read through column in csv file read country name, change name country code using iso 3166 alpha2 standard.

i have referenced pycountries: convert country names (possibly incomplete!) countrycodes, i'm having difficulty code.

basically, taking csv file has hundreds thousands of entries in column, need convert full name country code based on iso 3166 standard. have been playing around "pycountry" , various functions in python csv module, haven't been able work. want overwrite country name country code or @ least output new file. suggestions?

i using following rest of code, i'm not entirely sure how write country conversion want do:

import sys import csv import pycountry  csv_file = csv.dictreader(open(sys.argv[1], 'rb'))  column in csv_file:     x = column['name']... 

iterate on pycountry.countries , initialize mapping name -> short name (alpha2, or alpha3):

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

for file containing:

name kazakhstan ukraine 

it prints:

kazakhstan kz ukraine ua 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -