0

I would like to replace all spaces in string strings with '\s+'. Strings may have one or more spaces between words. The current code that I have tried is:

import nltk
string = 'jason   e   n robins inc'
tokenized = nltk.word_tokenize(string)
out = '\s+'.join(tokenized)
#expected: 'jason\s+e\s+n\s+robins\s+inc'
#get: 'jason\\s+e\\s+n\\s+robins\\s+inc'

The goal is to pass the output through a string parser:

import re
other_str = 'some other text where i want to split jason e n robins  inc off the end with other text'
final_output = re.split(other_text, out)[0]

Thanks for your help.

ktj1989
  • 679
  • 2
  • 8
  • 19

0 Answers0