dabbrev = dict() def funct(fname): dfile = open(fname, "r") for line in dfile: line = line.strip() words = line.split('\t') dabbrev[words[0]] = words[1] ##dkeys = dabbrev.keys() ##for k in dkeys: ## print(k, "->", dabbrev[k]) infile = open("abbrev-input.txt", "r") funct("abbreviations.txt") funct("abbreviations-num.txt") for line in infile: # print(line) words = line.split() # print(words) for word in words: if word in dabbrev: print(dabbrev[word], end=' ') else: print(word, end=' ') print('')