Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove empty lines with or without whitespace in Python

Tags:

python

string

People also ask

How do I remove blank lines from a string?

resultString = Regex. Replace(subjectString, @"^\s+$[\r\n]*", string. Empty, RegexOptions. Multiline);

Are Blank lines forbidden in Python?

Blank line is definitely allowed in python functions. All of yours examples (A,B,C,D) should work and the problem probably (sure) is related to "Eclipse and the PyDev plug-in".

How do I check if a line is empty in Python?

Using len() function To check an empty string in Python, use the len() function; if it returns 0, that means the string is empty; otherwise, it is not. So, if the string has something, it will count as a non-empty string; otherwise, it is an empty string.


Try list comprehension and string.strip():

>>> mystr = "L1\nL2\n\nL3\nL4\n  \n\nL5"
>>> mystr.split('\n')
['L1', 'L2', '', 'L3', 'L4', '  ', '', 'L5']
>>> [line for line in mystr.split('\n') if line.strip() != '']
['L1', 'L2', 'L3', 'L4', 'L5']

Using regex:

if re.match(r'^\s*$', line):
    # line is empty (has only the following: \t\n\r and whitespace)

Using regex + filter():

filtered = filter(lambda x: not re.match(r'^\s*$', x), original)

As seen on codepad.


I also tried regexp and list solutions, and list one is faster.

Here is my solution (by previous answers):

text = "\n".join([ll.rstrip() for ll in original_text.splitlines() if ll.strip()])

lines = bigstring.split('\n')
lines = [line for line in lines if line.strip()]

Surprised a multiline re.sub has not been suggested (Oh, because you've already split your string... But why?):

>>> import re
>>> a = "Foo\n \nBar\nBaz\n\n   Garply\n  \n"
>>> print a
Foo

Bar
Baz

        Garply


>>> print(re.sub(r'\n\s*\n','\n',a,re.MULTILINE))
Foo
Bar
Baz
        Garply

>>> 

If you are not willing to try regex (which you should), you can use this:

s.replace('\n\n','\n')

Repeat this several times to make sure there is no blank line left. Or chaining the commands:

s.replace('\n\n','\n').replace('\n\n','\n')


Just to encourage you to use regex, here are two introductory videos that I find intuitive:
• Regular Expressions (Regex) Tutorial
• Python Tutorial: re Module


you can simply use rstrip:

    for stuff in largestring:
        print(stuff.rstrip("\n")