regex to remove lines made of only whitespace

Chris Withers chris at simplistix.co.uk
Wed Aug 11 18:04:12 CEST 2010


Steven D'Aprano wrote:
> def strip_blank_lines(lines):
>     for line in lines:
>         if not line.isspace():
>             yield line
> 
> text = ''.join(strip_blank_lines(lines.split('\n')))

The final version I have is:

def strip_blank_lines(text):
     result = []
     for line in text.split('\n'):
         if line and not line.isspace():
             result.append(line)
     return '\n'.join(result)

Any improvements would be very welcome!

Chris




More information about the Python-list mailing list