[Tutor] Remove soft line break

Valerio Pachera valerio at pbds.eu
Thu Feb 28 07:05:27 EST 2019


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

  s.replace('\n ', '')

In other words removing a new line followed by a space.
In such case, the approach is not by line.

f = open('file.ics')
content=f.read().replace('\n ', '')
f.close()

The real use case is an .ics file.
It works as expected but there's a thing I've to understand about the end of line in general.

The original file line break is CR CF

cat --show-all file.ics
METHOD:PUBLISH^M$
BEGIN:VTIMEZONE^M$

I noticed that the end of file doesn't get preserve if I create a copy of the file by simply

f = open('file.ics')
content=f.read()
f.close()

f = open('file_copy.ics', 'w')
write('content')
f.close()

cat --show-all file_copy.ics
METHOD:PUBLISH$
BEGIN:VTIMEZONE$

What am I missing?


----- Messaggio originale -----
Da: "Peter Otten" <__peter__ at web.de>
A: "Tutor Python" <tutor at python.org>
Inviato: Lunedì, 4 febbraio 2019 20:23:59
Oggetto: Re: [Tutor] Remove soft line break

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"


More information about the Tutor mailing list