Python 如何在使用令牌更改文本空间后恢复文本空间?

Python 如何在使用令牌更改文本空间后恢复文本空间?,python,function,text,Python,Function,Text,我有原始文本和它的标记: 例如: 我是changin’tokens: 例如: Genius -> <spam>Genius</spam> Genius->Genius 我需要恢复包含空格的文本。 我怎么能做到? 结果应该是这样的: " <spam>Genius</spam>, scientist and my friend - John" “天才、科学家和我的朋友——约翰” 继续跟踪需要更改的令牌,并在原始

我有原始文本和它的标记: 例如:

我是changin’tokens: 例如:

Genius -> <spam>Genius</spam>
Genius->Genius
我需要恢复包含空格的文本。 我怎么能做到? 结果应该是这样的:

"  <spam>Genius</spam>,     scientist and my friend - John"
“天才、科学家和我的朋友——约翰”

继续跟踪需要更改的令牌,并在原始字符串中替换它们

new_text = original_text
replacements = {'Genius': '<spam>Genius</spam>'}
for orig in replacements:
  new_text = new_text.replace(orig, replacements[orig])
新文本=原始文本
替换={'Genius':'Genius'}
对于替换中的原始版本:
新建文本=新建文本。替换(原始,替换[原始])
new_text = original_text
replacements = {'Genius': '<spam>Genius</spam>'}
for orig in replacements:
  new_text = new_text.replace(orig, replacements[orig])