I have a text file with several lines and want to replace strings that match certain values. I opted to use a dictionary to store the string-to-be-replaced as the key, and the replacement as the value. The dictionary was created by opening another text file (template.txt) that has two columns separated by a single Tab:
140101002 name44 140101030 name2 140101004 name3 140101058 name94
The input file (input.txt) has the following text:
...text... <!-- 140101002 --> ...text...
...text... <!-- 140101030 --> ...text...
...text... <!-- 140101004 --> ...text...
...text... <!-- 140101058 --> ...text...
and I want to search for the strings in the template.txt first column (like 140101002) and replace it with the strings in the second column (like name44), so that the output file (output.txt) will look like this:
...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...
I have tried fixing this several times but cannot get it to work as intended:
fhand = open("/home/Documents/input.txt")
template = open("/home/Documents/template.txt")
fout = open("/home/Documents/output.txt","w")
# store the values in the dictionary:
templateDict = dict()
for line in template:
lineList = line.split("\t")
templateDict[lineList[0]] = lineList[1]
# replacement:
for line in fhand:
for key in templateDict:
if key in line2:
line2 = line2.replace(key,templateDict[key])
fout.write(line2) # write to output.txt
fout.close()