什么是一个快速班轮从一个python字符串中删除空行?(What's a quick one

2019-06-26 04:03发布

我有一个包含多余的空行一个Python字符串一些代码。 我想从字符串中删除所有空行。 什么是最Python的方式做到这一点?

注:我不是在寻找一个一般的代码重新格式化,只是一个快速的一个或两个衬垫。

谢谢!

Answer 1:

怎么样:

text = os.linesep.join([s for s in text.splitlines() if s])

其中, text是与可能无关行字符串?



Answer 2:

"\n".join([s for s in code.split("\n") if s])

EDIT2:

text = "".join([s for s in code.splitlines(True) if s.strip("\r\n")])

我认为这是我的最终版本。 它应该甚至混合代码行结束工作。 我不认为有空间,行应该被视为空的,但如果是这样那么简单s.strip()会做代替。



Answer 3:

filter(None, code.splitlines())
filter(str.strip, code.splitlines())

相当于

[s for s in code.splitlines() if s]
[s for s in code.splitlines() if s.strip()]

并且可能是有用的可读性



Answer 4:

ON课:除去换行符和用空格空行

“T”是与文本中的变量。 你会看到一个“s”变量,它只有主组括号的评估过程中存在的临时变量(忘了这些可爱的小东西蟒的名字)

首先让设置“T”变量,因此它有新的线路:

>>> t='hi there here is\na big line\n\nof empty\nline\neven some with spaces\n       \nlike that\n\n    \nokay now what?\n'

注意有另一种方式利用三引号来设置varible

somevar="""
   asdfas
asdf

  asdf

  asdf

asdf
""""

下面是它的外观时,我们认为它没有“打印”:

>>> t
'hi there here is\na big line\n\nof empty\nline\neven some with spaces\n       \nlike that\n\n    \nokay now what?\n' 

要看到实际的换行符,打印。

>>> print t
hi there here is
a big line

of empty
line
even some with spaces

like that


okay now what?

命令删除所有空行(包括空格):

所以somelines换行符只是换行,有的还有空间,使他们看起来像新线

如果你想摆脱所有的空行看的(如果他们刚刚换行,或还有空间)

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?

要么:

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n").strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?

注:在t.strip这条()分裂线(true)可以移除了所以它只是t.splitlines(真),但后来你的输出可以用额外的换行符(这样会移除最后的换行符)结束。 该带()的最后部分s.strip(“\ r \ n”)。带()和s.strip()是实际去除了换行和换行的空间。

命令删除所有空行(但不是空间的):

技术上线与空间不应该被认为是空的,但是这一切都取决于使用情况和你想要达到的目的。

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n")])
hi there here is
a big line
of empty
line
even some with spaces

like that

okay now what?

**注意有关MIDDLE条**

这中间条状有,这就是连接到“T”变量,只是删除了最后一个换行符(就像以前的笔记已注明)。 下面是它会是什么样子而没有带在那里(请注意,最后的换行符)

与第一实施例(除去新行和换行符用空格)

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n").strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
.without strip new line here (stackoverflow cant have me format it in).

与第二实施例(仅除去换行符)

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n")])
hi there here is
a big line
of empty
line
even some with spaces

like that

okay now what?
.without strip new line here (stackoverflow cant have me format it in).

结束!



Answer 5:

通过使用应用re.sub功能

re.sub(r'^$\n', '', s, flags=re.MULTILINE)


Answer 6:

这一次将删除空格行了。

re.replace(u'(?imu)^\s*\n', u'', code)



Answer 7:

这里是一个行的解决方案:

print("".join([s for s in mystr.splitlines(True) if s.strip()]))


Answer 8:

现在完全不同的东西:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string, re
>>> tidy = lambda s: string.join(filter(string.strip, re.split(r'[\r\n]+', s)), '\n')
>>> tidy('\r\n   \n\ra\n\n   b   \r\rc\n\n')
'a\012   b   \012c'

第2集:

这其中不上1.5 :-(工作

但它不仅处理通用换行和空白行,它还会删除尾随空白(好主意代码行恕我直言,整理时)和做了修复工作,如果最后一个有意义的线不被终止。

import re
tidy = lambda c: re.sub(
    r'(^\s*[\r\n]+|^\s*\Z)|(\s*\Z|\s*[\r\n]+)',
    lambda m: '\n' if m.lastindex == 2 else '',
    c)


Answer 9:

扩大对YMV的回答,您可以使用filterjoin ,从而获得所需的字符串,

"".join(filter(str.strip, sample_string.splitlines(True)))


文章来源: What's a quick one-liner to remove empty lines from a python string?