w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Deleting surplus blank lines using Python

Make sure there's no more than , eg:

import re
print re.sub('
{3,}', '

', your_string, flags=re.M)

And, using itertools.groupby for large files:

from itertools import groupby

with open('your_file') as fin:
    for has_value, lines in groupby(fin, lambda L: bool(L.strip())):
        if not has_value:
            print
            continue
        for line in lines:
            print line,




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.