Here is a copy paste of my answer on stackexchange forums

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:

<span class="pun">>>></span><span class="pln"> t</span><span class="pun">=</span><span class="str" style="color: #800000;">'hi there here is\na big line\n\nof empty\nline\neven some with spaces\n       \nlike that\n\n    \nokay now what?\n'</span>

Note there is another way to set the varible using triple quotes

<span class="pln">somevar</span><span class="pun">=</span><span class="str" style="color: #800000;">"""
   asdfas
asdf

  asdf

  asdf

asdf
""""</span>

Here is how it looks when we view it without “print”:

<span class="pun">>>></span><span class="pln"> t
</span><span class="str" style="color: #800000;">'hi there here is\na big line\n\nof empty\nline\neven some with spaces\n       \nlike that\n\n    \nokay now what?\n'</span>

To see with actual newlines, print it.

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="pln"> t
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line

of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces

like that


okay now what</span><span class="pun">?</span>

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)

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="str" style="color: #800000;">""</span><span class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span class="pln">s </span><span class="kwd" style="color: #00008b;">for</span><span class="pln"> s </span><span class="kwd" style="color: #00008b;">in</span><span class="pln"> t</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">().</span><span class="pln">splitlines</span><span class="pun">(</span><span class="kwd" style="color: #00008b;">True</span><span class="pun">)</span><span class="kwd" style="color: #00008b;">if</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">()])</span><span class="pln">
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line
of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces
like that
okay now what</span><span class="pun">?</span>

OR:

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="str" style="color: #800000;">""</span><span class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span class="pln">s </span><span class="kwd" style="color: #00008b;">for</span><span class="pln"> s </span><span class="kwd" style="color: #00008b;">in</span><span class="pln"> t</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">().</span><span class="pln">splitlines</span><span class="pun">(</span><span class="kwd" style="color: #00008b;">True</span><span class="pun">)</span><span class="kwd" style="color: #00008b;">if</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">(</span><span class="str" style="color: #800000;">"\r\n"</span><span class="pun">).</span><span class="pln">strip</span><span class="pun">()])</span><span class="pln">
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line
of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces
like that
okay now what</span><span class="pun">?</span>

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.

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="str" style="color: #800000;">""</span><span class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span class="pln">s </span><span class="kwd" style="color: #00008b;">for</span><span class="pln"> s </span><span class="kwd" style="color: #00008b;">in</span><span class="pln"> t</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">().</span><span class="pln">splitlines</span><span class="pun">(</span><span class="kwd" style="color: #00008b;">True</span><span class="pun">)</span><span class="kwd" style="color: #00008b;">if</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">(</span><span class="str" style="color: #800000;">"\r\n"</span><span class="pun">)])</span><span class="pln">
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line
of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces

like that

okay now what</span><span class="pun">?</span>

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

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="str" style="color: #800000;">""</span><span class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span class="pln">s </span><span class="kwd" style="color: #00008b;">for</span><span class="pln"> s </span><span class="kwd" style="color: #00008b;">in</span><span class="pln"> t</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">().</span><span class="pln">splitlines</span><span class="pun">(</span><span class="kwd" style="color: #00008b;">True</span><span class="pun">)</span><span class="kwd" style="color: #00008b;">if</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">(</span><span class="str" style="color: #800000;">"\r\n"</span><span class="pun">).</span><span class="pln">strip</span><span class="pun">()])</span><span class="pln">
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line
of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces
like that
okay now what</span><span class="pun">?</span><span class="pun">.</span><span class="pln">without strip new line here </span><span class="pun">(</span><span class="pln">stackoverflow cant have me format it </span><span class="kwd" style="color: #00008b;">in</span><span class="pun">).</span>

With 2nd example (removing newlines only)

<span class="pun">>>></span><span class="kwd" style="color: #00008b;">print</span><span class="str" style="color: #800000;">""</span><span class="pun">.</span><span class="pln">join</span><span class="pun">([</span><span class="pln">s </span><span class="kwd" style="color: #00008b;">for</span><span class="pln"> s </span><span class="kwd" style="color: #00008b;">in</span><span class="pln"> t</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">().</span><span class="pln">splitlines</span><span class="pun">(</span><span class="kwd" style="color: #00008b;">True</span><span class="pun">)</span><span class="kwd" style="color: #00008b;">if</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">strip</span><span class="pun">(</span><span class="str" style="color: #800000;">"\r\n"</span><span class="pun">)])</span><span class="pln">
hi there here </span><span class="kwd" style="color: #00008b;">is</span><span class="pln">
a big line
of empty
line
even some </span><span class="kwd" style="color: #00008b;">with</span><span class="pln"> spaces

like that

okay now what</span><span class="pun">?</span><span class="pun">.</span><span class="pln">without strip new line here </span><span class="pun">(</span><span class="pln">stackoverflow cant have me format it </span><span class="kwd" style="color: #00008b;">in</span><span class="pun">).</span>

The END!

Leave a Reply

Your email address will not be published. Required fields are marked *