是否有一个Python函数将从字符串中删除空白(空格和制表符)?

所以给定的输入" \t example string\t "变成了"example string"


当前回答

(re.sub(' +', ' ',(my_str。取代(' \ n ',' ')))). 带()

这将删除所有不需要的空格和换行符。希望这对你有所帮助

import re
my_str = '   a     b \n c   '
formatted_str = (re.sub(' +', ' ',(my_str.replace('\n',' ')))).strip()

这将导致:

'a b \n c'将被更改为'a bc '

其他回答

在以不同程度的理解看了相当多的解决方案之后,我想知道如果字符串是逗号分隔的该怎么办……

这个问题

在尝试处理一个csv的联系信息时,我需要解决这个问题:删除无关的空格和一些垃圾,但保留后面的逗号和内部空格。使用一个包含联系人注释的字段,我想删除垃圾,留下好的东西。修剪掉所有的标点符号和杂物,我不想失去复合标记之间的空白,因为我不想以后重新构建。

正则表达式和模式:[\s_]+?\ W +

该模式以[\s_]+?出现在从1到无限时间的非单词字符之前,使用这个:\W+(相当于[^a-zA-Z0-9_])。具体来说,它可以找到大量的空白:空字符(\0)、制表符(\t)、换行符(\n)、前馈(\f)、回车符(\r)。

我认为这样做有两个好处:

它不会删除你可能想要放在一起的完整单词/标记之间的空白; Python内置的字符串方法strip()不处理字符串内部,只处理左右两端,默认的arg是空字符(参见下面的示例:文本中有几个换行符,strip()不会删除它们,而regex模式会删除它们)。文本。带(' t \ r \ n \ ')

这超出了OPs的问题,但我认为在文本数据中有很多情况下我们可能会遇到奇怪的、病态的实例,就像我所做的那样(一些转义字符最终出现在一些文本中)。此外,在类似列表的字符串中,我们不希望消除分隔符,除非分隔符分隔了两个空白字符或一些非单词字符,如'- '或'-,,,,'。

注意:不是在谈论CSV本身的分隔符。仅适用于CSV中数据类似列表的实例,即子字符串组成的c.s.字符串。

Full disclosure: I've only been manipulating text for about a month, and regex only the last two weeks, so I'm sure there are some nuances I'm missing. That said, for smaller collections of strings (mine are in a dataframe of 12,000 rows and 40 odd columns), as a final step after a pass for removal of extraneous characters, this works exceptionally well, especially if you introduce some additional whitespace where you want to separate text joined by a non-word character, but don't want to add whitespace where there was none before.

一个例子:

import re


text = "\"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12,  2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , ,  dd invites,subscribed, , , , \r, , \0, ff dd \n invites, subscribed, , ,  , , alumni spring 2012 deck: https: www.dropbox.com s, \n i69rpofhfsp9t7c practice 20ignition - 20june \t\n .2134.pdf 2109                                                 \n\n\n\nklkjsdf\""

print(f"Here is the text as formatted:\n{text}\n")
print()
print("Trimming both the whitespaces and the non-word characters that follow them.")
print()
trim_ws_punctn = re.compile(r'[\s_]+?\W+')
clean_text = trim_ws_punctn.sub(' ', text)
print(clean_text)
print()
print("what about 'strip()'?")
print(f"Here is the text, formatted as is:\n{text}\n")
clean_text = text.strip(' \n\t\r')  # strip out whitespace?
print()
print(f"Here is the text, formatted as is:\n{clean_text}\n")

print()
print("Are 'text' and 'clean_text' unchanged?")
print(clean_text == text)

这个输出:

Here is the text as formatted:

"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12,  2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , ,  dd invites,subscribed, ,, , , ff dd 
 invites, subscribed, , ,  , , alumni spring 2012 deck: https: www.dropbox.com s, 
 i69rpofhfsp9t7c practice 20ignition - 20june 
 .2134.pdf 2109                                                 



klkjsdf" 

using regex to trim both the whitespaces and the non-word characters that follow them.

"portfolio, derp, hello-world, hello-, world, founders, mentors, ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, ff, series a, exit, general mailing, fr, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk,  jim.somedude@blahblah.com, dd invites,subscribed,, master, dd invites,subscribed, ff dd invites, subscribed, alumni spring 2012 deck: https: www.dropbox.com s, i69rpofhfsp9t7c practice 20ignition 20june 2134.pdf 2109 klkjsdf"

Very nice.
What about 'strip()'?

Here is the text, formatted as is:

"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12,  2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , ,  dd invites,subscribed, ,, , , ff dd 
 invites, subscribed, , ,  , , alumni spring 2012 deck: https: www.dropbox.com s, 
 i69rpofhfsp9t7c practice 20ignition - 20june 
 .2134.pdf 2109                                                 



klkjsdf"


Here is the text, after stipping with 'strip':


"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12,  2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , ,  dd invites,subscribed, ,, , , ff dd 
 invites, subscribed, , ,  , , alumni spring 2012 deck: https: www.dropbox.com s, 
 i69rpofhfsp9t7c practice 20ignition - 20june 
 .2134.pdf 2109                                                 



klkjsdf"
Are 'text' and 'clean_text' unchanged? 'True'

所以strip一次删除一个空白。所以在OPs的情况下,strip()是可以的。但如果事情变得更复杂,regex和类似的模式可能对更一般的设置有一定的价值。

看看它的实际应用

前导空格和尾随空格:

s = '   foo    \t   '
print s.strip() # prints "foo"

否则,正则表达式工作:

import re
pat = re.compile(r'\s+')
s = '  \t  foo   \t   bar \t  '
print pat.sub('', s) # prints "foobar"

对于两边的空格,使用str.strip:

s = "  \t a string example\t  "
s = s.strip()

对于右边的空白,使用str.rstrip:

s = s.rstrip()

对于左边的空白,使用str.lstrip:

s = s.lstrip()

你可以提供一个参数将任意字符剥离到这些函数中,如下所示:

s = s.strip(' \t\n\r')

这将从字符串两侧剥离任何空格、\t、\n或\r字符。

上面的例子只从字符串的左边和右边删除字符串。如果你也想从字符串中间删除字符,请尝试re.sub:

import re
print(re.sub('[\s+]', '', s))

这应该打印出来:

astringexample

(re.sub(' +', ' ',(my_str。取代(' \ n ',' ')))). 带()

这将删除所有不需要的空格和换行符。希望这对你有所帮助

import re
my_str = '   a     b \n c   '
formatted_str = (re.sub(' +', ' ',(my_str.replace('\n',' ')))).strip()

这将导致:

'a b \n c'将被更改为'a bc '

在Python中,trim方法被命名为strip:

str.strip()  # trim
str.lstrip()  # left trim
str.rstrip()  # right trim