用另一个文件中的另一个字符串替换一个文件中的字符串

时间:2018-10-17 03:52:35

标签: python string loops dictionary replace

我有一个包含多行的文本文件,想要替换与某些值匹配的字符串。我选择使用字典来存储要替换的字符串作为键,并将替换的字符串存储为值。通过打开另一个文本文件(template.txt)创建字典,该文本文件具有两列并由一个Tab分隔:

140101002   name44
140101030   name2
140101004   name3
140101058   name94

输入文件(input.txt)包含以下文本:

...text... <!-- 140101002 --> ...text...
...text... <!-- 140101030 --> ...text...
...text... <!-- 140101004 --> ...text...
...text... <!-- 140101058 --> ...text...

,我想搜索第一列template.txt中的字符串(如140101002),并将其替换为第二列中的字符串(如name44),以便输出文件(output.txt)将如下所示:

...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...

我已尝试修复此问题多次,但无法使其按预期工作:

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()

2 个答案:

答案 0 :(得分:2)

您可以将第一个文件读入词典,然后使用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))

输出:

...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...

答案 1 :(得分:0)

在将行拆分为列表之前,我必须去除换行符。

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()