我可以为您提供一个示例代码:```
定义一个字典,其中包含要替换的关键词
dict = { 'apple': '苹果', 'cat': '猫', 'dog': '狗' }# 读取文件内容 with open('file.txt', 'r') as f: lines = f.readlines()# 遍历每一行,替换关键词 for line in lines: for key in dict.keys(): line = line.replace(key, dict[key]) print(line)# 将替换后的结果写入新文 ```