What's a quick one-liner to remove empty lines from a python string? What's a quick one-liner to remove empty lines from a python string? python python

What's a quick one-liner to remove empty lines from a python string?


How about:

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

where text is the string with the possible extraneous lines?


"\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")])

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.


LESSON ON REMOVING NEWLINES and EMPTY LINES WITH SPACES

"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)

First lets set the "t" variable so it has new lines:

>>> 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="""   asdfasasdf  asdf  asdfasdf""""

Here is how it looks when we view it without "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 thi there here isa big lineof emptylineeven some with spaceslike thatokay 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 isa big lineof emptylineeven some with spaceslike thatokay now what?

OR:

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n").strip()])hi there here isa big lineof emptylineeven some with spaceslike thatokay 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.

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 isa big lineof emptylineeven some with spaceslike thatokay 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)

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 isa big lineof emptylineeven some with spaceslike thatokay now what?.without strip new line here (stackoverflow 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 isa big lineof emptylineeven some with spaceslike thatokay now what?.without strip new line here (stackoverflow cant have me format it in).

The END!