collapsing whitespace in a string

后端 未结 6 1848
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 03:59

I have a string that kind of looks like this:

\"stuff   .  // : /// more-stuff .. .. ...$%$% stuff -> DD\"

and I want to strip off all p

6条回答
  •  心在旅途
    2021-01-05 04:38

    result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
    result = rex.sub('', result) # this reduces all those spaces
    

    Because you typo'd and forgot to use rex_s for the second call instead. Also, you need to substitute at least one space back in or you'll end up with any multiple-space gap becoming no gap at all, instead of a single-space gap.

    result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
    result = rex_s.sub(' ', result) # this reduces all those spaces
    

提交回复
热议问题