w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can I format a txt file in python so that extra paragraph lines are removed as well as extra blank spaces?

The trick here is that you want to turn any sequence of 2 or more into exactly 2 characters. This is hard to write with just split and join—but it's dead simple to write with re.sub:

n = re.sub(r'

+', r'

', n)

If you want lines with nothing but spaces to be treated as blank lines, do this after stripping spaces; if you want them to be treated as non-blank, do it before.

You probably also want to change your space-stripping code to use split(' ') rather than just split(), so it doesn't screw up newlines. (You could also use re.sub for that as well, but it isn't really necessary, because turning 1 or more spaces into exactly 1 isn't hard to write with split and join.)

Alternatively, you could just go line by line, and keep track of the last line (either with an explicit variable inside the loop, or by writing a simple adjacent_pairs iterator, like i1, i2 = tee(ivar); next(i2); return zip_longest(i1, i2, fillvalue='')) and if the current line and the previous line are both blank, don't write the current line.

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