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.
Source: Python-3x Questions