我如何修剪空白?

有没有一个Python的function,将修剪string的空白(空格和制表符)?

示例: \t example string\texample string

两边空白:

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

空白在右侧:

 s = s.rstrip() 

空白在左边:

 s = s.lstrip() 

正如dd指出的那样,你可以提供一个参数来去除任意这些函数的任意字符,如下所示:

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

这将从string的左侧,右侧或两侧剥去任何空格, \t\n\r字符。

上面的示例仅从string的左侧和右侧移除string。 如果您还想从string中删除字符,请尝试re.sub

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

这应该打印出来:

 astringexample 

Python trim方法称为strip

 str.strip() #trim str.lstrip() #ltrim str.rstrip() #rtrim 

对于前导和尾随空格:

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

否则,正则expression式工作:

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

你也可以使用非常简单的基本function: str.replace() ,使用空格和制表符:

 >>> whitespaces = " abcd ef gh ijkl " >>> tabs = " abcde fgh ijkl" >>> print whitespaces.replace(" ", "") abcdefghijkl >>> print tabs.replace(" ", "") abcdefghijkl 

简单和容易。

 #how to trim a multi line string or a file s=""" line one \tline two\t line three """ #line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space. s1=s.splitlines() print s1 [' line one', '\tline two\t', 'line three '] print [i.strip() for i in s1] ['line one', 'line two', 'line three'] #more details: #we could also have used a forloop from the begining: for line in s.splitlines(): line=line.strip() process(line) #we could also be reading a file line by line.. eg my_file=open(filename), or with open(filename) as myfile: for line in my_file: line=line.strip() process(line) #moot point: note splitlines() removed the newline characters, we can keep them by passing True: #although split() will then remove them anyway.. s2=s.splitlines(True) print s2 [' line one\n', '\tline two\t\n', 'line three '] 

没有人发布这些正则expression式解决scheme。

匹配:

 >>> import re >>> p=re.compile('\\s*(.*\\S)?\\s*') >>> m=p.match(' \t blah ') >>> m.group(1) 'blah' >>> m=p.match(' \tbl ah \t ') >>> m.group(1) 'bl ah' >>> m=p.match(' \t ') >>> print m.group(1) None 

search(您必须以不同的方式处理“唯一空格”input案例):

 >>> p1=re.compile('\\S.*\\S') >>> m=p1.search(' \tblah \t ') >>> m.group() 'blah' >>> m=p1.search(' \tbl ah \t ') >>> m.group() 'bl ah' >>> m=p1.search(' \t ') >>> m.group() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'group' 

如果你使用re.sub ,你可能会删除内部的空白,这可能是不可取的。

空格包括空格,制表符和CRLF 。 所以我们可以使用一个优雅的单行string函数来翻译

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

或者如果你想彻底

 import string ' hello apple'.translate(None, string.whitespace) 
  something = "\t please_ \t remove_ all_ \n\n\n\nwhitespaces\n\t " something = "".join(something.split()) 

输出:please_remove_all_whitespaces

这将从string的开头和结尾删除所有的空格和换行符:

 >>> s = " \n\t \n some \n text \n " >>> re.sub("^\s+|\s+$", "", s) >>> "some \n text" 

尝试翻译

 >>> import string >>> print '\t\r\n hello \r\n world \t\r\n' hello world >>> tr = string.maketrans(string.whitespace, ' '*len(string.whitespace)) >>> '\t\r\n hello \r\n world \t\r\n'.translate(tr) ' hello world ' >>> '\t\r\n hello \r\n world \t\r\n'.translate(tr).replace(' ', '') 'helloworld' 

一般来说,我使用下面的方法:

 >>> myStr = "Hi\n Stack Over \r flow!" >>> charList = [u"\u005Cn",u"\u005Cr",u"\u005Ct"] >>> import re >>> for i in charList: myStr = re.sub(i, r"", myStr) >>> myStr 'Hi Stack Over flow' 

注意:这仅用于删除“\ n”,“\ r”和“\ t”。 它不会删除多余的空间。

 content = "this is \nText\r\r\t\n. This is new text" 

要删除\n\r\t ,更好的方法是:

 data = "" for i in content: data += i.strip("\n").strip("\t").strip("\r").replace("\n","").replace("\t","").replace("\r","") 

输出:

 >>> data 'this is Text. This is new text' 

这是删除上述字符最简单的方法。 如果有任何python包或库可用,那么请让我知道,并build议如何删除字符/ ??,由于按Enter键发生。