collapsing whitespace in a string

后端 未结 6 1871
隐瞒了意图╮
隐瞒了意图╮ 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:48

    Here's a single-step approach (but the uppercasing actually uses a string method -- much simpler!):

    rex = re.compile(r'\W+')
    result = rex.sub(' ', strarg).upper()
    

    where strarg is the string argument (don't use names that shadow builtins or standard library modules, please).

提交回复
热议问题