在Python中,可以使用re模块来实现批量替换文本内容。下面是一个简单的示例代码:
import redef batch_replace_text(input_text, replacements): for old_str, new_str in replacements.itEMS(): input_text = re.sub(old_str, new_str, input_text) return input_textif __name__ == "__main__": text = "Hello, world! This is a test text for batch replacement." replacements = { r"Hello": "Hi", r"world": "Python", r"test": "example" } new_text = batch_replace_text(text, replacements) print(new_text)在上面的示例中,batch_replace_text函数接收一个输入文本input_text和一个包含需要替换的字符串及替换后的新字符串的字典replacements。然后使用re.sub函数来实现批量替换文本内容。最后输出替换后的新文本。


