import re regex = re.compile('^(\d+) (.*)$') pad = '' filename = 'descriptions.txt' file = open(filename, 'r') print('descriptions = {') for line in file: line = line[:-1] match = regex.match(line) if match: code = match.group(1) description = match.group(2) #description = description.replace("'", "\'") print(" %s'%s' : \"%s\"," % (pad, code, description)) print('}') file.close()