删除字符串中的所有空格

发布于 2024-12-18 12:19:07 字数 217 浏览 2 评论 0原文

我想消除字符串两端和单词之间的所有空格。

我有这个Python代码:

def my_handle(self):
    sentence = ' hello  apple  '
    sentence.strip()

但这只能消除字符串两侧的空格。如何删除所有空格?

I want to eliminate all the whitespace from a string, on both ends, and in between words.

I have this Python code:

def my_handle(self):
    sentence = ' hello  apple  '
    sentence.strip()

But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

祁梦 2024-12-25 12:19:07

如果要删除开头和结尾的空格,请使用 str.strip ()

>>> "  hello  apple  ".strip()
'hello  apple'

如果要删除所有空格字符,请使用 str.replace() (注意,这只会删除“正常”ASCII 空格字符 ' ' U+0020 但不是任何其他空格)

>>> "  hello  apple  ".replace(" ", "")
'helloapple'

如果您想删除所有空格,然后之间留一个空格字符的话,使用 str.split() 后跟 str.join()

>>> " ".join("  hello  apple  ".split())
'hello apple'

如果要删除所有 空格然后更改上面的内容将 " " 引导至 ""

>>> "".join("  hello  apple  ".split())
'helloapple'

If you want to remove leading and ending whitespace, use str.strip():

>>> "  hello  apple  ".strip()
'hello  apple'

If you want to remove all space characters, use str.replace() (NB this only removes the “normal” ASCII space character ' ' U+0020 but not any other whitespace):

>>> "  hello  apple  ".replace(" ", "")
'helloapple'

If you want to remove all whitespace and then leave a single space character between words, use str.split() followed by str.join():

>>> " ".join("  hello  apple  ".split())
'hello apple'

If you want to remove all whitespace then change the above leading " " to "":

>>> "".join("  hello  apple  ".split())
'helloapple'
眼藏柔 2024-12-25 12:19:07

要仅删除空格,请使用 str.replace

sentence = sentence.replace(' ', '')

要删除所有空白字符(空格、制表符、换行符等),您可以使用 分割,然后join

sentence = ''.join(sentence.split())

或正则表达式:

import re
pattern = re.compile(r'\s+')
sentence = re.sub(pattern, '', sentence)

如果您只想删除开头和结尾的空格,可以使用<一href="http://docs.python.org/2/library/stdtypes.html#str.strip">strip

sentence = sentence.strip()

您还可以使用 lstrip 仅删除字符串开头的空格,以及 rstrip 从字符串末尾删除空格。

To remove only spaces use str.replace:

sentence = sentence.replace(' ', '')

To remove all whitespace characters (space, tab, newline, and so on) you can use split then join:

sentence = ''.join(sentence.split())

or a regular expression:

import re
pattern = re.compile(r'\s+')
sentence = re.sub(pattern, '', sentence)

If you want to only remove whitespace from the beginning and end you can use strip:

sentence = sentence.strip()

You can also use lstrip to remove whitespace only from the beginning of the string, and rstrip to remove whitespace from the end of the string.

朦胧时间 2024-12-25 12:19:07

另一种方法是使用正则表达式并匹配这些奇怪的空白字符。以下是一些示例:

删除字符串中的所有空格,甚至单词之间的空格:

import re
sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)

删除字符串开头的空格

import re
sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)

删除字符串结尾的空格:

import re
sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)

删除字符串开头和结尾处的空格:

import re
sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)

仅删除重复的空格:

import re
sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))

(所有示例都适用于 Python 2 和 Python 3)

An alternative is to use regular expressions and match these strange white-space characters too. Here are some examples:

Remove ALL whitespace in a string, even between words:

import re
sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)

Remove whitespace in the BEGINNING of a string:

import re
sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)

Remove whitespace in the END of a string:

import re
sentence = re.sub(r"\s+
quot;, "", sentence, flags=re.UNICODE)

Remove whitespace both at the BEGINNING and at the END of a string:

import re
sentence = re.sub("^\s+|\s+
quot;, "", sentence, flags=re.UNICODE)

Remove ONLY DUPLICATE whitespace:

import re
sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))

(All examples work in both Python 2 and Python 3)

下壹個目標 2024-12-25 12:19:07

“空白”包括空格、制表符和CRLF。因此,我们可以使用的一个优雅的单行字符串函数是 str.translate

Python 3

' hello  apple '.translate(str.maketrans('', '', ' \n\t\r'))

OR(如果你想彻底):

import string
' hello  apple'.translate(str.maketrans('', '', string.whitespace))

Python 2

' hello  apple'.translate(None, ' \n\t\r')

OR(如果你想彻底)彻底:

import string
' hello  apple'.translate(None, string.whitespace)

"Whitespace" includes space, tabs, and CRLF. So an elegant and one-liner string function we can use is str.translate:

Python 3

' hello  apple '.translate(str.maketrans('', '', ' \n\t\r'))

OR if you want to be thorough:

import string
' hello  apple'.translate(str.maketrans('', '', string.whitespace))

Python 2

' hello  apple'.translate(None, ' \n\t\r')

OR if you want to be thorough:

import string
' hello  apple'.translate(None, string.whitespace)
老街孤人 2024-12-25 12:19:07

要删除开头和结尾的空格,请使用 strip

>> "  foo bar   ".strip()
"foo bar"

For removing whitespace from beginning and end, use strip.

>> "  foo bar   ".strip()
"foo bar"
故乡的云 2024-12-25 12:19:07
' hello  \n\tapple'.translate({ord(c):None for c in ' \n\t\r'})

MaK在上面已经指出了“翻译”的方法。此变体适用于 Python 3(请参阅 此问答)。

' hello  \n\tapple'.translate({ord(c):None for c in ' \n\t\r'})

MaK already pointed out the "translate" method above. And this variation works with Python 3 (see this Q&A).

暮年 2024-12-25 12:19:07

此外,strip 有一些变化:

删除开头和中的空格字符串结尾:

sentence= sentence.strip()

删除字符串开头的空格:

sentence = sentence.lstrip()

删除字符串末尾的空格:

sentence= sentence.rstrip()

所有三个字符串函数 strip< /代码> lstriprstrip 可以获取要剥离的字符串参数,默认为全空格。当您处理特定内容时,这会很有帮助,例如,您可以仅删除空格而不是换行符:

" 1. Step 1\n".strip(" ")

或者您可以在读取字符串列表时删除多余的逗号:

"1,2,3,".strip(",")

In addition, strip has some variations:

Remove spaces in the BEGINNING and END of a string:

sentence= sentence.strip()

Remove spaces in the BEGINNING of a string:

sentence = sentence.lstrip()

Remove spaces in the END of a string:

sentence= sentence.rstrip()

All three string functions strip lstrip, and rstrip can take parameters of the string to strip, with the default being all white space. This can be helpful when you are working with something particular, for example, you could remove only spaces but not newlines:

" 1. Step 1\n".strip(" ")

Or you could remove extra commas when reading in a string list:

"1,2,3,".strip(",")
我们的影子 2024-12-25 12:19:07

请注意:

strip 执行 rstrip 和 lstrip(删除前导和尾随空格、制表符、回车符和换页符,但不会删除字符串中间的它们)。

如果您只替换空格和制表符,您最终可能会得到隐藏的 CRLF,这些 CRLF 看起来与您要查找的内容匹配,但并不相同。

Be careful:

strip does a rstrip and lstrip (removes leading and trailing spaces, tabs, returns and form feeds, but it does not remove them in the middle of the string).

If you only replace spaces and tabs you can end up with hidden CRLFs that appear to match what you are looking for, but are not the same.

孤星 2024-12-25 12:19:07

消除字符串两端以及单词之间的所有空格。

>>> import re
>>> re.sub("\s+", # one or more repetition of whitespace
    '', # replace with empty string (->remove)
    ''' hello
...    apple
... ''')
'helloapple'

Python 文档:

eliminate all the whitespace from a string, on both ends, and in between words.

>>> import re
>>> re.sub("\s+", # one or more repetition of whitespace
    '', # replace with empty string (->remove)
    ''' hello
...    apple
... ''')
'helloapple'

Python docs:

时光清浅 2024-12-25 12:19:07

我使用 split() 忽略所有空格并使用 join() 来连接
字符串。

sentence = ''.join(' hello  apple  '.split())
print(sentence) #=> 'helloapple'

我更喜欢这种方法,因为它只是一个表达式(而不是语句)。
它易于使用,无需绑定变量即可使用。

print(''.join(' hello  apple  '.split())) # no need to binding to a variable

I use split() to ignore all whitespaces and use join() to concatenate
strings.

sentence = ''.join(' hello  apple  '.split())
print(sentence) #=> 'helloapple'

I prefer this approach because it is only a expression (not a statement).
It is easy to use and it can use without binding to a variable.

print(''.join(' hello  apple  '.split())) # no need to binding to a variable
听闻余生 2024-12-25 12:19:07
import re    
sentence = ' hello  apple'
re.sub(' ','',sentence) #helloworld (remove all spaces)
re.sub('  ',' ',sentence) #hello world (remove double spaces)
import re    
sentence = ' hello  apple'
re.sub(' ','',sentence) #helloworld (remove all spaces)
re.sub('  ',' ',sentence) #hello world (remove double spaces)
别闹i 2024-12-25 12:19:07

在下面的脚本中,我们导入正则表达式模块,用于用一个空格替换一个或多个空格。这确保了内部多余的空间被删除。然后我们使用 strip() 函数删除前导和尾随空格。

# Import regular expression module
import re

# Initialize string
a = "     foo      bar   "

# First replace any number of spaces with a single space
a = re.sub(' +', ' ', a)

# Then strip any leading and trailing spaces.
a = a.strip()

# Show results
print(a)

In the following script we import the regular expression module which we use to substitute one space or more with a single space. This ensures that the inner extra spaces are removed. Then we use strip() function to remove leading and trailing spaces.

# Import regular expression module
import re

# Initialize string
a = "     foo      bar   "

# First replace any number of spaces with a single space
a = re.sub(' +', ' ', a)

# Then strip any leading and trailing spaces.
a = a.strip()

# Show results
print(a)
爱*していゐ 2024-12-25 12:19:07

Python 3 中所有字符串字符都是 unicode 文字;因此,由于 str.split()所有空白字符上进行拆分,这意味着它会在 unicode 空白字符上进行拆分。所以 split + join 语法(如
1
2
3) 将产生与带有 UNICODE 标志的 re.sub 相同的输出(如 < a href="https://stackoverflow.com/a/28607213/19123103">4);事实上,UNICODE 标志在这里是多余的(如
2
5
6
7)。

import re
import sys

# all unicode characters
sentence = ''.join(map(chr, range(sys.maxunicode+1)))

# remove all white space characters
x = ''.join(sentence.split())
y = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
z = re.sub(r"\s+", "", sentence)

x == y == z      # True

在性能方面,由于Python的字符串方法经过了优化,因此它们比正则表达式快得多。正如下面的 timeit 测试所示,当从 OP 中的字符串中删除所有空白字符时,Python 字符串方法比 re 选项快 7 倍以上。

import timeit

import timeit

setup = """
import re
s = ' hello  \t apple  '
"""

t1 = min(timeit.repeat("''.join(s.split())", setup))
t2 = min(timeit.repeat("re.sub(r'\s+', '', s, flags=re.UNICODE)", setup))


t2 / t1  # 7.868004799367726

All string characters are unicode literal in Python 3; as a consequence, since str.split() splits on all white space characters, that means it splits on unicode white space characters. So split + join syntax (as in
1,
2,
3) will produce the same output as re.sub with the UNICODE flag (as in 4); in fact, the UNICODE flag is redundant here (as in
2,
5,
6,
7).

import re
import sys

# all unicode characters
sentence = ''.join(map(chr, range(sys.maxunicode+1)))

# remove all white space characters
x = ''.join(sentence.split())
y = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
z = re.sub(r"\s+", "", sentence)

x == y == z      # True

In terms of performance, since Python's string methods are optimized, they are much faster than regex. As the following timeit test shows, when removing all white space characters from the string in the OP, Python string methods are over 7 times faster than re option.

import timeit

import timeit

setup = """
import re
s = ' hello  \t apple  '
"""

t1 = min(timeit.repeat("''.join(s.split())", setup))
t2 = min(timeit.repeat("re.sub(r'\s+', '', s, flags=re.UNICODE)", setup))


t2 / t1  # 7.868004799367726
本宫微胖 2024-12-25 12:19:07

我发现这对我来说最有效:

test_string = '  test   a   s   test '
string_list = [s.strip() for s in str(test_string).split()]
final_string = ' '.join(string_array)
# final_string: 'test a s test'

它删除了所有空格、制表符等。

I found that this works the best for me:

test_string = '  test   a   s   test '
string_list = [s.strip() for s in str(test_string).split()]
final_string = ' '.join(string_array)
# final_string: 'test a s test'

It removes any whitespaces, tabs, etc.

ヤ经典坏疍 2024-12-25 12:19:07

只是除了 Emil Stenström 的答案

这段代码会删除所有空格,您也可以删除自己的额外 utf-8 字符。

import re

def utf8trim(s: str) -> str:
    spaces = "|".join([r"\s", "\u2800", "\u3164", "\u1160", "\uFFA0", "\u202c"])
    return re.sub(f"^[{spaces}]+|[{spaces}]+$", "", s, flags=re.UNICODE)

Just addition to the Emil Stenström's answer

This code removes all white spaces and you could also remove your own extra utf-8 characters.

import re

def utf8trim(s: str) -> str:
    spaces = "|".join([r"\s", "\u2800", "\u3164", "\u1160", "\uFFA0", "\u202c"])
    return re.sub(f"^[{spaces}]+|[{spaces}]+
quot;, "", s, flags=re.UNICODE)
清晨说晚安 2024-12-25 12:19:07

试试这个..而不是使用 re 我认为使用 split 和 strip 更好

def my_handle(self):
    sentence = ' hello  apple  '
    ' '.join(x.strip() for x in sentence.split())
#hello apple
    ''.join(x.strip() for x in sentence.split())
#helloapple

try this.. instead of using re i think using split with strip is much better

def my_handle(self):
    sentence = ' hello  apple  '
    ' '.join(x.strip() for x in sentence.split())
#hello apple
    ''.join(x.strip() for x in sentence.split())
#helloapple
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文