0

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()
1
  • 1
    Is there any error you are getting ? Also i notice in the replacement loop, it should be line instead of line2 Commented Oct 17, 2018 at 3:58

2 Answers 2

2

You can read the first file into a dictionary, and then use re.sub:

import re
d = dict(re.split('\s+', i.strip('\n')) for i in open('filename.txt'))
content = [i.strip('\n') for i in open('filename2.txt')]
with open('results.txt', 'w') as f:
  f.write('\n'.join(re.sub('(?<=\<\!\-\-\s)\d+(?=\s\-\-\>)', lambda x:d[x.group()], i) for i in content))

Output:

...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...
Sign up to request clarification or add additional context in comments.

Comments

1

I had to strip the newline character before splitting the line into a list.

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.strip("\n").split("\t")
  templateDict[lineList[0]] = lineList[1]

# replacement:
for line in fhand:
  for key in templateDict:
    if key in line:
      line = line.replace(key,templateDict[key])
  fout.write(line) # write to output.txt

fout.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.