[Tutor] Remove soft line break

Peter Otten __peter__ at web.de
Fri Mar 1 10:17:57 EST 2019


Valerio Pachera wrote:

[Me:]

>> def merge_lines(lines):
>>     lines = (line.rstrip("\n") for line in lines)
>>     accu = [next(lines)]
>>     for line in lines:
>>         if line.startswith(" "):
>>             accu.append(line[1:])
>>         else:
>>             yield "".join(accu) + "\n"
>>             accu = [line]
>>     yield "".join(accu) + "\n"

> I think it could be solved in a much easier way.

> content=f.read().replace('\n ', '')

That is indeed much simpler than my suggestion.

> What am I missing?

Nothing. My attempt to keep memory usage low led to a solution which is more 
complex than your slurp-it-in, fix it, spit-it-out.



More information about the Tutor mailing list