How to remove tabs and newlines with a regex
问题 In Python 3.x, the special re sequence '\s' matches Unicode whitespace characters including [ \t\n\r\f\v]. The following piece of code is intended to replace tabs and newlines with a space. import re text = """Hello my friends. How are you doing? I'm fine.""" output = re.sub('\s', ' ', text) print(output) However, the tab is still present in output. Why? 回答1: The problem is(likely) that your tab character is just a bunch of spaces. >>> re.sub(r"\s+", " ", text) "Hello my friends. How are you