0

I've got original text and its tokens: Example:

original_text = "  Genius,     scientist and my friend - John"
text_tokens = ["Genius", ",", "scientist", "and", "my", "friend", "-", "John"]

I'm changin' tokens: For example:

Genius -> <spam>Genius</spam>

I need to recover text including SPACES. How I can do that? Result should be like that:

"  <spam>Genius</spam>,     scientist and my friend - John"
Blazing_Sun
  • 93
  • 1
  • 1
  • 6

1 Answers1

0

Keep tracking what tokens need to be changed, and replace them in the original string.

new_text = original_text
replacements = {'Genius': '<spam>Genius</spam>'}
for orig in replacements:
  new_text = new_text.replace(orig, replacements[orig])
igrinis
  • 12,398
  • 20
  • 45