用python3根据字典对文件关键词进行替换,并用代码展示
时间: 2023-03-06 07:55:33 浏览: 43
python入小课_代码实现30个.pdf
您可以使用下面的代码:with open('input.txt') as infile, open('output.txt', 'w') as outfile:
replacements = {
'keyword1': 'value1',
'keyword2': 'value2',
'keyword3': 'value3',
}
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
阅读全文