我如何剥离所有的空间在一个python字符串?例如,我想要一个像stripmyspaces这样的字符串变成stripmyspaces,但我似乎不能用strip()来完成:

>>> 'strip my spaces'.strip()
'strip my spaces'

当前回答

将字符串分成单词 去掉两边的空白 最后用单一的空间将它们连接起来

代码的最后一行:

' '.join(word.strip() for word in message_text.split()

其他回答

对于Python 3:

>>> import re
>>> re.sub(r'\s+', '', 'strip my \n\t\r ASCII and \u00A0 \u2003 Unicode spaces')
'stripmyASCIIandUnicodespaces'
>>> # Or, depending on the situation:
>>> re.sub(r'(\s|\u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF)+', '', \
... '\uFEFF\t\t\t strip all \u000A kinds of \u200B whitespace \n')
'stripallkindsofwhitespace'

...处理任何你没有想到的空白字符——相信我们,有很多。

\s本身总是覆盖ASCII空白:

(定期)空间 选项卡 新行(\n) 回车(\r) 换页 垂直制表符

另外:

对于启用了re.UNICODE的Python 2, Python 3,无需任何额外操作,

...\s还包括Unicode空白字符,例如:

插入空格, 他们的空间, 表意的空间,

…等。在“带有White_Space属性的Unicode字符”下面可以看到完整的列表。

但是\s不覆盖不属于空格的字符,这些字符实际上是空格,例如:

任意工匠, 蒙古语元音分隔符, 零宽度不间断空格(又称字节顺序标记),

…等。请在“没有White_Space属性的相关Unicode字符”下面查看完整列表。

所以这6个字符包含在第二个正则表达式的列表中,\u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF。

来源:

https://docs.python.org/2/library/re.html https://docs.python.org/3/library/re.html https://en.wikipedia.org/wiki/Unicode_character_property

TL /博士

这个解决方案使用Python 3.6进行了测试

在Python3中,要去除字符串中的所有空格,可以使用以下函数:

def remove_spaces(in_string: str):
    return in_string.translate(str.maketrans({' ': ''})

要删除任何空白字符(' \t\n\r\x0b\x0c'),您可以使用以下函数:

import string
def remove_whitespace(in_string: str):
    return in_string.translate(str.maketrans(dict.fromkeys(string.whitespace)))

解释

Python的str.translate方法是str的内置类方法,它接受一个表,并返回通过传递的转换表映射的每个字符的字符串副本。str.translate的完整文档

要创建转换表,使用str.maketrans。这个方法是str的另一个内置类方法。在这里,我们只用一个形参来使用它,在这种情况下是一个字典,其中的键是要替换的字符映射到字符替换值的值。它返回一个用于str.translate的翻译表。str.maketrans的完整文档

python中的string模块包含一些常见的字符串操作和常量。字符串。whitespace是一个常量,返回一个包含所有被认为是空格的ASCII字符的字符串。这包括字符空格、制表符、换行符、返回符、换行符和垂直制表符。string.whitespace的完整文档

在第二个函数中,dict.fromkeys用于创建一个字典,其中键是string返回的字符串中的字符。每个值为None的空格。dict.fromkeys的完整文档

如果不需要最佳性能,你只想要一些非常简单的东西,你可以定义一个基本函数来测试每个字符,使用string类内置的"isspace"方法:

def remove_space(input_string):
    no_white_space = ''
    for c in input_string:
        if not c.isspace():
            no_white_space += c
    return no_white_space

以这种方式构建no_white_space字符串不会有理想的性能,但解决方案很容易理解。

>>> remove_space('strip my spaces')
'stripmyspaces'

如果不想定义函数,可以将其转换为与列表推导式略有相似的内容。借用顶部答案的连接解决方案:

>>> "".join([c for c in "strip my spaces" if not c.isspace()])
'stripmyspaces'

最简单的是使用replace:

"foo bar\t".replace(" ", "").replace("\t", "")

或者,使用正则表达式:

import re
re.sub(r"\s", "", "foo bar\t")

筛选列表的标准技术适用,尽管它们不如拆分/连接或转换方法有效。

我们需要一组空白:

>>> import string
>>> ws = set(string.whitespace)

内置过滤器:

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'

一个列表推导式(是的,使用括号:参见下面的基准测试):

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'

折叠:

>>> import functools
>>> "".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))
'stripmyspaces'

基准:

>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995