Mark Baldwin Mark Baldwin - 1 month ago 10
Python Question

Key substitution in python

I have two text files, one we'll call

keys
and it looks something like this:

S-84
S-72
S-73
S-83
32
S-73
S-83
32
S-65
32
S-84
S-69
S-83
S-84
S-49


The other file is sort of a dictionary of key-value pairs:

S-49 : "!"
S-65 : "a"
S-66 : "b"
S-67 : "c"
S-68 : "d"
S-69 : "e"
S-70 : "f"
S-71 : "g"
S-72 : "h"
S-73 : "i"
S-74 : "j"
S-75 : "k"
S-76 : "l"
S-77 : "m"
S-78 : "n"
S-79 : "o"
S-80 : "p"
S-81 : "q"
S-82 : "r"
S-83 : "s"
S-84 : "t"
S-85 : "u"
S-86 : "v"
S-87 : "w"
S-88 : "x"
S-89 : "y"
S-90 : "z"
32 : " "


I want to read in the first file and replace the keys therein with their values in the second file so that the output file looks like this:

this is a test!


I don't really know where to start with this.
I was trying to start with something like this manually, but my output is
bleh


with open('newkey.log', 'r') as input_file, open('newkey.txt', 'w') as output_file:
for line in input_file:
if line.strip() == 'S-84':
output_file.write('t\n')
else:
output_file.write('bleh\n')


I think I am going to have to read-in the second file as a dictionary as well, or I could hard-code it in? I would prefer to end up being able to change that file outside of the interpreter.

Answer Source

Yes, you must first read in the second file to create your dictionary.

import re

d['S-186'] = ':'  # To account for the delimiter per your comments below.
with open(key_values_filename, 'r') as f:
    for row in f:
        k, v = row.split(':')
        d[k.strip()] = re.sub('^"|"$', '', v.strip())

Then read the other file and get the value from the matched key.

missing_value = 'key missing'
with open(keys_filename, 'r') as fin, open(result_filename, 'w') as fout:
    for row in fin:
        fout.write(d.get(row.strip(), missing_value))

For an explanation of the regular expression re.sub('^"|"$', '', v.strip()), it removes double quotations found either at the start or end of each parsed string stripped of whitespace.

  • ^" asserts position of the quotation at the start of the string.
  • "$ asserts position of the quotation at the end of the string.
  • | matches either assertion above.

The above solution works on your sample data.