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
join—but it's dead simple to write with
n = re.sub(r'
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
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.