删除字符串中的所有空格
- 2024-12-05 08:38:00
- admin 原创
- 149
问题描述:
我想消除字符串两端以及单词之间的所有空格。
我有这个 Python 代码:
def my_handle(self):
sentence = ' hello apple '
sentence.strip()
但这只能消除字符串两边的空格。如何删除所有空格?
解决方案 1:
如果要删除前导和结束空格,请使用str.strip()
:
>>> " hello apple ".strip()
'hello apple'
如果要删除所有空格字符,请使用(注意:这只会删除“正常”的 ASCII 空格字符,而不会删除任何其他空格):str.replace()
' ' U+0020
>>> " hello apple ".replace(" ", "")
'helloapple'
如果要删除所有空格然后在单词之间保留一个空格字符,请使用str.split()
后跟str.join()
:
>>> " ".join(" hello apple ".split())
'hello apple'
如果要删除所有空格,请将上述内容更改" "
为""
:
>>> "".join(" hello apple ".split())
'helloapple'
解决方案 2:
要仅删除空格,请使用str.replace
:
sentence = sentence.replace(' ', '')
要删除所有空白字符(空格、制表符、换行符等),您可以使用split
:join
sentence = ''.join(sentence.split())
或正则表达式:
import re
pattern = re.compile(r's+')
sentence = re.sub(pattern, '', sentence)
如果只想删除开头和结尾的空格,可以使用strip
:
sentence = sentence.strip()
您还可以使用lstrip
它仅从字符串开头删除空格,以及rstrip
从字符串末尾删除空格。
解决方案 3:
另一种方法是使用正则表达式并匹配这些奇怪的空白字符。以下是一些示例:
删除字符串中的所有空格,甚至单词之间的空格:
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)
解决方案 4:
“空白”包括空格、制表符和 CRLF。因此,我们可以使用一个优雅的单行字符串函数str.translate
:
Python 3
' hello apple '.translate(str.maketrans('', '', '
'))
或者如果你想要更彻底一些:
import string
' hello apple'.translate(str.maketrans('', '', string.whitespace))
Python 2
' hello apple'.translate(None, '
')
或者如果你想要更彻底一些:
import string
' hello apple'.translate(None, string.whitespace)
解决方案 5:
要删除开头和结尾的空格,请使用strip
。
>> " foo bar ".strip()
"foo bar"
解决方案 6:
' hello
apple'.translate({ord(c):None for c in '
'})
MaK 已经指出了上面的“翻译”方法。并且此变体适用于 Python 3(请参阅此问答)。
解决方案 7:
此外,strip还有一些变化:
删除字符串开头和结尾处的空格:
sentence= sentence.strip()
删除字符串开头的空格:
sentence = sentence.lstrip()
删除字符串末尾的空格:
sentence= sentence.rstrip()
所有三个字符串函数strip
lstrip
和rstrip
都可以接受要删除的字符串的参数,默认为全部删除空格。这在处理某些特定内容时非常有用,例如,您可以只删除空格而不删除换行符:
" 1. Step 1
".strip(" ")
或者,您可以在读取字符串列表时删除多余的逗号:
"1,2,3,".strip(",")
解决方案 8:
当心:
strip
执行 rstrip 和 lstrip(删除前导和尾随空格、制表符、回车符和换页符,但不会在字符串中间删除它们)。
如果您仅替换空格和制表符,您最终可能会得到隐藏的 CRLF,它们看起来与您要查找的内容相匹配,但并不相同。
解决方案 9:
消除字符串两端以及单词之间的所有空格。
>>> import re
>>> re.sub("s+", # one or more repetition of whitespace
'', # replace with empty string (->remove)
''' hello
... apple
... ''')
'helloapple'
Python 文档:
解决方案 10:
我使用 split() 忽略所有空格并使用 join() 连接字符串。
sentence = ''.join(' hello apple '.split())
print(sentence) #=> 'helloapple'
我更喜欢这种方法,因为它只是一个表达式(而不是语句)。
它易于使用,并且无需绑定到变量即可使用。
print(''.join(' hello apple '.split())) # no need to binding to a variable
解决方案 11:
import re
sentence = ' hello apple'
re.sub(' ','',sentence) #helloworld (remove all spaces)
re.sub(' ',' ',sentence) #hello world (remove double spaces)
解决方案 12:
在以下脚本中,我们导入正则表达式模块,使用该模块将一个或多个空格替换为单个空格。这可确保删除内部多余的空格。然后我们使用 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)
解决方案 13:
在 Python 3 中,所有字符串字符都是 unicode 文字;因此,由于str.split()
按所有空格字符进行拆分,这意味着按 unicode 空格字符进行拆分。因此split
+join
语法(如
1、
2、
3)将产生re.sub
与 UNICODE 标志相同的输出(如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 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
解决方案 14:
我发现这对我来说最有效:
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'
它会删除所有空格、制表符等等。
解决方案 15:
只是对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)
解决方案 16:
试试这个.. 我认为使用 split with strip 比使用 re 更好
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
扫码咨询,免费领取项目管理大礼包!