我想用' \ s +'替换字符串字符串中的所有空格。字符串可以在单词之间有一个或多个空格。我尝试过的当前代码是:
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'
目标是通过字符串解析器传递输出:
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]
感谢您的帮助。