I have a list of countries like:
countries=[\'American Samoa\', \'Canada\', \'France\'...]
I want to convert them like this:
You can use this csv file : country code list into a CSV.
import csv
dic = {}
with open("wikipedia-iso-country-codes.csv") as f:
file= csv.DictReader(f, delimiter=',')
for line in file:
dic[line['English short name lower case']] = line['Alpha-2 code']
countries = ['American Samoa', 'Canada', 'France']
for country in countries:
print(dic[country])
Will print:
AS
CA
FR
Few more alternatives.