Trying to make a python dictionary from a file but I keep getting errors like 'too many values to unpack' -


i have text file saved in notepad moved python folder has 3 letter acronym country on left , 4 or 5 spaces right has country corresponds so:

afg afghanistan
arm armenia
etc.

i need dictionary use 3 letters key , country value. has every country participates in olympics. here's code looks far:

def country(filename):     infile = open(filename,'r')     countrydict = {}     line in infile:         key,value = line.split()         countrydict[key] = value     print(countrydict)     return countrydict country('countrycodes.txt') 

most of countries (e.g. new zealand) have more 1 word in names , split() returning more 2 items, you're trying assign results 2 variables regardless. limit split one:

key, value = line.split(none, 1) 

if find you're getting excess whitespace @ end, throw strip() in there:

key, value = line.strip().split(none, 1) 

Comments