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

Question:

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?

Note: I’m not looking for a general code re-formatter, just a quick one or two-liner.

Thanks!

Asked By: Andrew Wagner

||

Answers:

How about:

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

where text is the string with the possible extraneous lines?

Answered By: Lawrence Johnston
"n".join([s for s in code.split("n") if s])

Edit2:

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

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.

Answered By: Wojciech Bederski

This one will remove lines of spaces too.

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

Answered By: peterdemin
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

Answered By: ymv

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'[rn]+', s)), 'n')
>>> tidy('rn   nrann   b   rrcnn')
'a12   b   12c'

Episode 2:

This one doesn’t work on 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*[rn]+|^s*Z)|(s*Z|s*[rn]+)',
    lambda m: 'n' if m.lastindex == 2 else '',
    c)
Answered By: John Machin

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 isna big linennof emptynlineneven some with spacesn       nlike thatnn    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”:

>>> t
'hi there here isna big linennof emptynlineneven some with spacesn       nlike thatnn    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("rn").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(“rn”).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("rn")])
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)

With 1st example (removing newlines and newlines with spaces)

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("rn").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).

With 2nd example (removing newlines only)

>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("rn")])
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).

The END!

Answered By: kossboss

Here is a one line solution:

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

By using re.sub function

re.sub(r'^$n', '', s, flags=re.MULTILINE)
Answered By: fbessho

expanding on ymv’s answer, you can use filter with join to get desired string,

"".join(filter(str.strip, sample_string.splitlines(True)))
Answered By: Sufiyan Ghori

This code removes empty lines (with or without whitespaces).

import re    
re.sub(r'ns*n', 'n', text, flags=re.MULTILINE)
Answered By: rfedorov

I wanted to remove a bunch of empty lines and what worked for me was:

if len(line) > 2:
    myfile.write(output)

I went with 2 since that covered the rn.
I did want a few empty rows just to make my formatting look better so in those cases I had to use:

print("    n"
Answered By: Ben

IMHO shortest and most Pythonic would be:

str(textWithEmptyLines).replace('nn','')
Answered By: Nariman Huseynov

using regex
re.sub(r'^$n', '', somestring, flags=re.MULTILINE)

Answered By: Shahnawaz Shaikh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.