collapsing whitespace in a string

后端 未结 6 1876
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  萌比男神i
    2021-01-05 04:48

    Do you have to use regular expressions? Do you feel you must do it in one line?

    >>> import string
    >>> s = "stuff   .  // : /// more-stuff .. .. ...$%$% stuff -> DD"
    >>> s2 = ''.join(c for c in s if c in string.letters + ' ')
    >>> ' '.join(s2.split())
    'stuff morestuff stuff DD'
    

提交回复
热议问题