Skip to content
Advertisement

Reduce multiple blank lines to single (Pythonically)

How can I reduce multiple blank lines in a text file to a single line at each occurrence?

I have read the entire file into a string, because I want to do some replacement across line endings.

with open(sourceFileName, 'rt') as sourceFile:
    sourceFileContents = sourceFile.read()

This doesn’t seem to work

while 'nnn' in sourceFileContents:
    sourceFileContents = sourceFileContents.replace('nnn', 'nn')

and nor does this

sourceFileContents = re.sub('nnn+', 'nn', sourceFileContents)

It’s easy enough to strip them all, but I want to reduce multiple blank lines to a single one, each time I encounter them.

I feel that I’m close, but just can’t get it to work.

Advertisement

Answer

This is a reach, but perhaps some of the lines aren’t completely blank (i.e. they have only whitespace characters that give the appearance of blankness). You could try removing all possible whitespace between newlines.

re.sub(r'(ns*)+n+', 'nn', sourceFileContents)

Edit: realized the second ‘+’ was superfluous, as the s* will catch newlines between the first and last. We just want to make sure the last character is definitely a newline so we don’t remove leading whitespace from a line with other content.

re.sub(r'(ns*)+n', 'nn', sourceFileContents)

Edit 2

re.sub(r'ns*n', 'nn', sourceFileContents)

Should be an even simpler solution. We really just want to a catch any possible space (which includes intermediate newlines) between our two anchor newlines that will make the single blank line and collapse it down to just the two newlines.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement