Python 将一个文件中的字符串替换为另一个文件中的另一个字符串
我有一个包含几行的文本文件,希望替换与某些值匹配的字符串。我选择使用字典来存储要替换的字符串作为键,替换的字符串作为值。字典是通过打开另一个文本文件(Python 将一个文件中的字符串替换为另一个文件中的另一个字符串,python,string,loops,dictionary,replace,Python,String,Loops,Dictionary,Replace,我有一个包含几行的文本文件,希望替换与某些值匹配的字符串。我选择使用字典来存储要替换的字符串作为键,替换的字符串作为值。字典是通过打开另一个文本文件(template.txt)创建的,该文件有两列,由单个选项卡分隔: 140101002 name44 140101030 name2 140101004 name3 140101058 name94 我想在template.txt第一列(如140101002)中搜索字符串,并将其替换为第二列(如name44)中的字符串,以便输出文件
template.txt
)创建的,该文件有两列,由单个选项卡分隔:
140101002 name44
140101030 name2
140101004 name3
140101058 name94
我想在template.txt
第一列(如140101002
)中搜索字符串,并将其替换为第二列(如name44
)中的字符串,以便输出文件(output.txt
)如下所示:
...text... <!-- name44 --> ...text...
...text... <!-- name2 --> ...text...
...text... <!-- name3 --> ...text...
...text... <!-- name94 --> ...text...
您可以将第一个文件读入词典,然后使用
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))
重新导入
d=dict(在open('filename.txt')中为i重新拆分('\s+',i.strip('\n'))
content=[i.strip('\n')表示打开的i('filename2.txt')]
将open('results.txt','w')作为f:
f、 write('\n'.join(re.sub)(?您可以将第一个文件读入字典,然后使用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))
重新导入
d=dict(在open('filename.txt')中为i重新拆分('\s+',i.strip('\n'))
content=[i.strip('\n')表示打开的i('filename2.txt')]
将open('results.txt','w')作为f:
f、 write('\n'.join(re.sub)(?在将行拆分为列表之前,我必须去掉换行符
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()
在将换行符拆分成一个列表之前,我必须去掉换行符
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()
是否有任何错误?我注意到在替换循环中,应该是line而不是line2。是否有任何错误?我注意到在替换循环中,应该是line而不是line2
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()