问题:如何从字符串中删除所有空格

如何删除python字符串中的所有空格?例如,我希望将一个字符串strip my spaces转换成stripmyspaces,但是我似乎无法通过以下方式完成此操作strip()

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

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot seem to accomplish that with strip():

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

回答 0

利用没有sep参数的str.split的行为:

>>> s = " \t foo \n bar "
>>> "".join(s.split())
'foobar'

如果只想删除空格而不是所有空格:

>>> s.replace(" ", "")
'\tfoo\nbar'

过早的优化

尽管效率不是主要目标(编写清晰的代码是),但以下是一些初始时间:

$ python -m timeit '"".join(" \t foo \n bar ".split())'
1000000 loops, best of 3: 1.38 usec per loop
$ python -m timeit -s 'import re' 're.sub(r"\s+", "", " \t foo \n bar ")'
100000 loops, best of 3: 15.6 usec per loop

请注意,正则表达式已缓存,因此它没有您想象的那么慢。编译事前帮助一些,但在实践中,如果你把这个只会重要很多倍:

$ python -m timeit -s 'import re; e = re.compile(r"\s+")' 'e.sub("", " \t foo \n bar ")'
100000 loops, best of 3: 7.76 usec per loop

即使re.sub慢了11.3倍,但请记住,您的瓶颈肯定会在其他地方。大多数程序不会注意到这三个选择之间的区别。

Taking advantage of str.split’s behavior with no sep parameter:

>>> s = " \t foo \n bar "
>>> "".join(s.split())
'foobar'

If you just want to remove spaces instead of all whitespace:

>>> s.replace(" ", "")
'\tfoo\nbar'

Premature optimization

Even though efficiency isn’t the primary goal—writing clear code is—here are some initial timings:

$ python -m timeit '"".join(" \t foo \n bar ".split())'
1000000 loops, best of 3: 1.38 usec per loop
$ python -m timeit -s 'import re' 're.sub(r"\s+", "", " \t foo \n bar ")'
100000 loops, best of 3: 15.6 usec per loop

Note the regex is cached, so it’s not as slow as you’d imagine. Compiling it beforehand helps some, but would only matter in practice if you call this many times:

$ python -m timeit -s 'import re; e = re.compile(r"\s+")' 'e.sub("", " \t foo \n bar ")'
100000 loops, best of 3: 7.76 usec per loop

Even though re.sub is 11.3x slower, remember your bottlenecks are assuredly elsewhere. Most programs would not notice the difference between any of these 3 choices.


回答 1

>>> import re
>>> re.sub(r'\s+', '', 'strip my spaces')
'stripmyspaces'

还可以处理您不会想到的所有空白字符(相信我,有很多)。

For 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'

…handles any whitespace characters that you’re not thinking of – and believe us, there are plenty.

\s on its own always covers the ASCII whitespace:

  • (regular) space
  • tab
  • new line (\n)
  • carriage return (\r)
  • form feed
  • vertical tab

Additionally:

  • for Python 2 with re.UNICODE enabled,
  • for Python 3 without any extra actions,

\s also covers the Unicode whitespace characters, for example:

  • non-breaking space,
  • em space,
  • ideographic space,

…etc. See the full list here, under “Unicode characters with White_Space property”.

However \s DOES NOT cover characters not classified as whitespace, which are de facto whitespace, such as among others:

  • zero-width joiner,
  • Mongolian vowel separator,
  • zero-width non-breaking space (a.k.a. byte order mark),

…etc. See the full list here, under “Related Unicode characters without White_Space property”.

So these 6 characters are covered by the list in the second regex, \u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF.

Sources:


回答 2

或者,

"strip my spaces".translate( None, string.whitespace )

这是Python3版本:

"strip my spaces".translate(str.maketrans('', '', string.whitespace))

Alternatively,

"strip my spaces".translate( None, string.whitespace )

And here is Python3 version:

"strip my spaces".translate(str.maketrans('', '', string.whitespace))

回答 3

最简单的方法是使用replace:

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

或者,使用正则表达式:

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

The simplest is to use replace:

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

Alternatively, use a regular expression:

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

回答 4

在Python中删除起始空间

string1="    This is Test String to strip leading space"
print string1
print string1.lstrip()

在Python中删除尾随或结尾空格

string2="This is Test String to strip trailing space     "
print string2
print string2.rstrip()

在Python中从字符串的开头和结尾删除空格

string3="    This is Test String to strip leading and trailing space      "
print string3
print string3.strip()

删除python中的所有空格

string4="   This is Test String to test all the spaces        "
print string4
print string4.replace(" ", "")

Remove the Starting Spaces in Python

string1="    This is Test String to strip leading space"
print string1
print string1.lstrip()

Remove the Trailing or End Spaces in Python

string2="This is Test String to strip trailing space     "
print string2
print string2.rstrip()

Remove the whiteSpaces from Beginning and end of the string in Python

string3="    This is Test String to strip leading and trailing space      "
print string3
print string3.strip()

Remove all the spaces in python

string4="   This is Test String to test all the spaces        "
print string4
print string4.replace(" ", "")

回答 5

尝试使用regex re.sub。您可以搜索所有空格并替换为空字符串。

\s模式中的匹配空格字符-不仅是空格(制表符,换行符等)。您可以在手册中了解更多信息。

Try a regex with re.sub. You can search for all whitespace and replace with an empty string.

\s in your pattern will match whitespace characters – and not just a space (tabs, newlines, etc). You can read more about it in the manual.


回答 6

import re
re.sub(' ','','strip my spaces')
import re
re.sub(' ','','strip my spaces')

回答 7

如Roger Pate所述,以下代码为我工作:

s = " \t foo \n bar "
"".join(s.split())
'foobar'

我正在使用Jupyter Notebook运行以下代码:

i=0
ProductList=[]
while i < len(new_list): 
   temp=''                            # new_list[i]=temp=' Plain   Utthapam  '
   #temp=new_list[i].strip()          #if we want o/p as: 'Plain Utthapam'
   temp="".join(new_list[i].split())  #o/p: 'PlainUtthapam' 
   temp=temp.upper()                  #o/p:'PLAINUTTHAPAM' 
   ProductList.append(temp)
   i=i+2

As mentioned by Roger Pate following code worked for me:

s = " \t foo \n bar "
"".join(s.split())
'foobar'

I am using Jupyter Notebook to run following code:

i=0
ProductList=[]
while i < len(new_list): 
   temp=''                            # new_list[i]=temp=' Plain   Utthapam  '
   #temp=new_list[i].strip()          #if we want o/p as: 'Plain Utthapam'
   temp="".join(new_list[i].split())  #o/p: 'PlainUtthapam' 
   temp=temp.upper()                  #o/p:'PLAINUTTHAPAM' 
   ProductList.append(temp)
   i=i+2

回答 8

可以使用过滤列表的标准技术,尽管它们不如split/jointranslate方法有效。

我们需要一组空格:

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

filter内置:

>>> "".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

The standard techniques to filter a list apply, although they are not as efficient as the split/join or translate methods.

We need a set of whitespaces:

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

The filter builtin:

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

A list comprehension (yes, use the brackets: see benchmark below):

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

A fold:

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

Benchmark:

>>> 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

回答 9

TL / DR

该解决方案已使用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.translatestr.maketrans的完整文档

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

在第二个函数dict.fromkeys中,用于创建字典,其中的键是string.whitespace每个带有value 的字符串返回的字符Nonedict.fromkeys的完整文档

TL/DR

This solution was tested using Python 3.6

To strip all spaces from a string in Python3 you can use the following function:

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

To remove any whitespace characters (‘ \t\n\r\x0b\x0c’) you can use the following function:

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

Explanation

Python’s str.translate method is a built-in class method of str, it takes a table and returns a copy of the string with each character mapped through the passed translation table. Full documentation for str.translate

To create the translation table str.maketrans is used. This method is another built-in class method of str. Here we use it with only one parameter, in this case a dictionary, where the keys are the characters to be replaced mapped to values with the characters replacement value. It returns a translation table for use with str.translate. Full documentation for str.maketrans

The string module in python contains some common string operations and constants. string.whitespace is a constant which returns a string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.Full documentation for string

In the second function dict.fromkeys is used to create a dictionary where the keys are the characters in the string returned by string.whitespace each with value None. Full documentation for dict.fromkeys


回答 10

如果不是最佳性能的要求,而您只想简单地做一些事情,则可以使用字符串类的内置“ 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解决方案中借用:

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

If optimal performance is not a requirement and you just want something dead simple, you can define a basic function to test each character using the string class’s built in “isspace” method:

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

Building the no_white_space string this way will not have ideal performance, but the solution is easy to understand.

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

If you don’t want to define a function, you can convert this into something vaguely similar with list comprehension. Borrowing from the top answer’s join solution:

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。