I have some code in a python string that contains extraneous empty lines. I would like to remove all empty lines from the string. What's the most pythonic way to do this?
我在python字符串中有一些包含无关空行的代码。我想从字符串中删除所有空行。什么是最pythonic的方式来做到这一点?
Note: I'm not looking for a general code re-formatter, just a quick one or two-liner.
注意:我不是在寻找一般的代码重新格式化程序,只需要一个快速的单行或双行。
Thanks!
谢谢!
8 个解决方案
#1
62
How about:
怎么样:
text = os.linesep.join([s for s in text.splitlines() if s])
where text
is the string with the possible extraneous lines?
text是带有可能无关的行的字符串?
#2
12
"\n".join([s for s in code.split("\n") if s])
Edit2:
EDIT2:
text = "".join([s for s in code.splitlines(True) if s.strip("\r\n")])
I think that's my final version. It should work well even with code mixing line endings. I don't think that line with spaces should be considered empty, but if so then simple s.strip() will do instead.
我认为这是我的最终版本。即使使用代码混合行结尾,它也应该可以正常工作。我不认为带空格的行应该被认为是空的,但是如果是这样的话,那么简单的s.strip()就可以了。
#3
10
filter(None, code.splitlines())
filter(str.strip, code.splitlines())
are equivalent to
相当于
[s for s in code.splitlines() if s]
[s for s in code.splitlines() if s.strip()]
and might be useful for readability
并且可能对可读性有用
#4
8
LESSON ON REMOVING NEWLINES and EMPTY LINES WITH SPACES
关于用空间去除NEWLINES和EMPTY LINES的课程
"t" is the variable with the text. You will see an "s" variable, its a temporary variable that only exists during the evaluation of the main set of parenthesis (forgot the name of these lil python things)
“t”是带有文本的变量。你会看到一个“s”变量,它是一个临时变量,只在评估主要括号的过程中存在(忘了这些lil python东西的名字)
First lets set the "t" variable so it has new lines:
首先设置“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'
Note there is another way to set the varible using triple quotes
请注意,还有另一种使用三引号设置变量的方法
somevar="""
asdfas
asdf
asdf
asdf
asdf
""""
Here is how it looks when we view it without "print":
以下是我们在没有“print”时查看它的样子:
>>> 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'
To see with actual newlines, print it.
要查看实际换行符,请将其打印出来。
>>> print t
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
COMMAND REMOVE ALL BLANK LINES (INCLUDING SPACES):
命令删除所有空白线(包括空格):
So somelines newlines are just newlines, and some have spaces so they look like new lines
所以某些换行符只是新行,有些则有空格,所以它们看起来像新行
If you want to get rid of all blank looking lines (if they have just newlines, or spaces as well)
如果你想摆脱所有空白行(如果他们只有换行符或空格)
>>> 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?
OR:
要么:
>>> 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?
NOTE: that strip in t.strip().splitline(True) can be removes so its just t.splitlines(True), but then your output can end with an extra newline (so that removes the final newline). The strip() in the last part s.strip("\r\n").strip() and s.strip() is what actually removes the spaces in newlines and newlines.
注意:t.strip()。splitline(True)中的条带可以被删除,因此它只是t.splitlines(True),但是输出可以以额外的换行结束(这样就删除了最后的换行符)。最后一部分s.strip(“\ r \ n”)。strip()和s.strip()中的strip()实际上是删除换行符和换行符中的空格。
COMMAND REMOVE ALL BLANK LINES (BUT NOT ONES WITH SPACES):
命令删除所有空白线(但不包括空格):
Technically lines with spaces should NOT be considered empty, but it all depends on the use case and what your trying to achieve.
从技术上讲,带空格的行不应该被认为是空的,但这一切都取决于用例和你想要达到的目的。
>>> 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?
** NOTE ABOUT THAT MIDDLE strip **
**关于中间带**的说明
That middle strip there, thats attached to the "t" variable, just removes the last newline (just as the previous note has stated). Here is how it would look like without that strip being there (notice that last newline)
那个中间条带,那附加到“t”变量,只是删除了最后一个换行符(正如前面的说明所述)。如果没有那条带在那里它会是这样的(注意最后一条换行符)
With 1st example (removing newlines and newlines with spaces)
第一个例子(用空格删除换行符和换行符)
>>> 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 (* cant have me format it in).
With 2nd example (removing newlines only)
第二个例子(仅删除换行符)
>>> 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 (* cant have me format it in).
The END!
结束!
#5
3
This one will remove lines of spaces too.
这个也将删除空格行。
re.replace(u'(?imu)^\s*\n', u'', code)
re.replace(你'(?imu)^ \ s * \ n',你',代码)
#7
0
And now for something completely different:
而现在完全不同的东西:
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'
Episode 2:
第2集:
This one doesn't work on 1.5 :-(
这个不适用于1.5 :-(
BUT not only does it handle universal newlines and blank lines, it also removes trailing whitespace (good idea when tidying up code lines IMHO) AND does a repair job if the last meaningful line is not terminated.
但它不仅处理通用换行符和空白行,它还删除尾随空格(在整理代码行恕我直言时的好主意)并在最后有意义的行未终止时执行修复工作。
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)
#8
0
Here is a one line solution:
print("".join([s for s in mystr.splitlines(True) if s.strip()]))
#1
62
How about:
怎么样:
text = os.linesep.join([s for s in text.splitlines() if s])
where text
is the string with the possible extraneous lines?
text是带有可能无关的行的字符串?
#2
12
"\n".join([s for s in code.split("\n") if s])
Edit2:
EDIT2:
text = "".join([s for s in code.splitlines(True) if s.strip("\r\n")])
I think that's my final version. It should work well even with code mixing line endings. I don't think that line with spaces should be considered empty, but if so then simple s.strip() will do instead.
我认为这是我的最终版本。即使使用代码混合行结尾,它也应该可以正常工作。我不认为带空格的行应该被认为是空的,但是如果是这样的话,那么简单的s.strip()就可以了。
#3
10
filter(None, code.splitlines())
filter(str.strip, code.splitlines())
are equivalent to
相当于
[s for s in code.splitlines() if s]
[s for s in code.splitlines() if s.strip()]
and might be useful for readability
并且可能对可读性有用
#4
8
LESSON ON REMOVING NEWLINES and EMPTY LINES WITH SPACES
关于用空间去除NEWLINES和EMPTY LINES的课程
"t" is the variable with the text. You will see an "s" variable, its a temporary variable that only exists during the evaluation of the main set of parenthesis (forgot the name of these lil python things)
“t”是带有文本的变量。你会看到一个“s”变量,它是一个临时变量,只在评估主要括号的过程中存在(忘了这些lil python东西的名字)
First lets set the "t" variable so it has new lines:
首先设置“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'
Note there is another way to set the varible using triple quotes
请注意,还有另一种使用三引号设置变量的方法
somevar="""
asdfas
asdf
asdf
asdf
asdf
""""
Here is how it looks when we view it without "print":
以下是我们在没有“print”时查看它的样子:
>>> 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'
To see with actual newlines, print it.
要查看实际换行符,请将其打印出来。
>>> print t
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
COMMAND REMOVE ALL BLANK LINES (INCLUDING SPACES):
命令删除所有空白线(包括空格):
So somelines newlines are just newlines, and some have spaces so they look like new lines
所以某些换行符只是新行,有些则有空格,所以它们看起来像新行
If you want to get rid of all blank looking lines (if they have just newlines, or spaces as well)
如果你想摆脱所有空白行(如果他们只有换行符或空格)
>>> 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?
OR:
要么:
>>> 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?
NOTE: that strip in t.strip().splitline(True) can be removes so its just t.splitlines(True), but then your output can end with an extra newline (so that removes the final newline). The strip() in the last part s.strip("\r\n").strip() and s.strip() is what actually removes the spaces in newlines and newlines.
注意:t.strip()。splitline(True)中的条带可以被删除,因此它只是t.splitlines(True),但是输出可以以额外的换行结束(这样就删除了最后的换行符)。最后一部分s.strip(“\ r \ n”)。strip()和s.strip()中的strip()实际上是删除换行符和换行符中的空格。
COMMAND REMOVE ALL BLANK LINES (BUT NOT ONES WITH SPACES):
命令删除所有空白线(但不包括空格):
Technically lines with spaces should NOT be considered empty, but it all depends on the use case and what your trying to achieve.
从技术上讲,带空格的行不应该被认为是空的,但这一切都取决于用例和你想要达到的目的。
>>> 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?
** NOTE ABOUT THAT MIDDLE strip **
**关于中间带**的说明
That middle strip there, thats attached to the "t" variable, just removes the last newline (just as the previous note has stated). Here is how it would look like without that strip being there (notice that last newline)
那个中间条带,那附加到“t”变量,只是删除了最后一个换行符(正如前面的说明所述)。如果没有那条带在那里它会是这样的(注意最后一条换行符)
With 1st example (removing newlines and newlines with spaces)
第一个例子(用空格删除换行符和换行符)
>>> 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 (* cant have me format it in).
With 2nd example (removing newlines only)
第二个例子(仅删除换行符)
>>> 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 (* cant have me format it in).
The END!
结束!
#5
3
This one will remove lines of spaces too.
这个也将删除空格行。
re.replace(u'(?imu)^\s*\n', u'', code)
re.replace(你'(?imu)^ \ s * \ n',你',代码)
#6
#7
0
And now for something completely different:
而现在完全不同的东西:
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'
Episode 2:
第2集:
This one doesn't work on 1.5 :-(
这个不适用于1.5 :-(
BUT not only does it handle universal newlines and blank lines, it also removes trailing whitespace (good idea when tidying up code lines IMHO) AND does a repair job if the last meaningful line is not terminated.
但它不仅处理通用换行符和空白行,它还删除尾随空格(在整理代码行恕我直言时的好主意)并在最后有意义的行未终止时执行修复工作。
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)
#8
0
Here is a one line solution:
print("".join([s for s in mystr.splitlines(True) if s.strip()]))