Remove whitespace in Python using string.whitespace

后端 未结 5 615
夕颜
夕颜 2020-11-29 19:51

Python\'s string.whitespace is great:

>>> string.whitespace
\'\\t\\n\\x0b\\x0c\\r \'

How do I use this with a string without resor

5条回答
  •  遥遥无期
    2020-11-29 20:40

    What's wrong with the \s character class?

    >>> import re
    
    >>> pattern = re.compile(r'\s+')
    >>> re.sub(pattern, ' ', "Please \n don't \t hurt \x0b me.")
    "Please don't hurt me."
    

提交回复
热议问题