# a global dictionary to store post codes postcodes = {} def readcodes(filename): ''' Read post codes from and store in the global variable ''' global postcodes # read the file and split them by lines pcs = open(filename, 'rU').read().split('\n') # split each line by a colon (":"), allowing 1 split max postcodes = dict( [ pc.strip().split(':', 1) for pc in pcs if pc.strip() ] ) def lookupcode(code): return postcodes.get(code, 'Unknown')