[Tutor] Remove soft line break

Peter Otten __peter__ at web.de
Mon Feb 4 14:23:59 EST 2019


Valerio Pachera wrote:

> 
> I have a file with row that split at the 80th character.
> The next row start with a blank space, meaning that i part of the previous
> row.
> 
> Example:
> 
> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non justo
> enim. Viv
>  amus dapibus quis neque vitae ornare. Pellentesque at pharetra sapien, id
>  eleif end lacus. Nullam ut semper enim, vulputate venenatis justo.
>  Vestibulum vehicul a dolor sit amet ultricies vulputate. Aenean lobortis,
>  nulla eu scelerisque hen
> 
> What do you suggest to get the text on a single line?

Neglecting the corner cases:

$ cat mergelines.py 
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"


SAMPLE =  """\
foo bar baz
  ham spam
alpha beta
  gamma
 delta
  epsilon
""".splitlines(True)

for line in merge_lines(SAMPLE):
    print(line, end="")
$ python3 mergelines.py 
foo bar baz ham spam
alpha beta gammadelta epsilon

I hope this isn't homework ;)




More information about the Tutor mailing list