[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

TextWrapper keepking line breaks?

js

3/10/2008 11:31:00 PM

Hi list,

Can I make TextWrapper keep line breaks in the text?

For example,

>>> s = "spam\nham"
>>> print wrap(s)
spam
ham

As far as I can tell, there seems no way to do this,
but before writing my own solution, I want to know whether
the solution already exists or not.

Thanks.
2 Answers

Arnaud Delobelle

3/11/2008 1:28:00 AM

0

On Mar 10, 11:31 pm, js <ebgs...@gmail.com> wrote:
> Hi list,
>
> Can I make TextWrapper keep line breaks in the text?
>
> For example,
>
> >>> s = "spam\nham"
> >>> print wrap(s)
>
> spam
> ham
>
> As far as I can tell, there seems no way to do this,
> but before writing my own solution, I want to know whether
> the solution already exists or not.
>
> Thanks.

Don't know but you could write:

>>> import textwrap
>>> def wraplines(text):
... return '\n'.join(textwrap.fill(line) for line in
text.split('\n'))
...
>>> s = "spam\nham"
>>> print wraplines(s)
spam
ham
>>>

HTH

--
Arnaud

js

3/11/2008 12:49:00 PM

0

Hi Arnaud,

Great. Thanks for your help!

On Tue, Mar 11, 2008 at 10:27 AM, Arnaud Delobelle
<arnodel@googlemail.com> wrote:
>
> On Mar 10, 11:31 pm, js <ebgs...@gmail.com> wrote:
> > Hi list,
> >
> > Can I make TextWrapper keep line breaks in the text?
> >
> > For example,
> >
> > >>> s = "spam\nham"
> > >>> print wrap(s)
> >
> > spam
> > ham
> >
> > As far as I can tell, there seems no way to do this,
> > but before writing my own solution, I want to know whether
> > the solution already exists or not.
> >
> > Thanks.
>
> Don't know but you could write:
>
> >>> import textwrap
> >>> def wraplines(text):
> ... return '\n'.join(textwrap.fill(line) for line in
> text.split('\n'))
> ...
> >>> s = "spam\nham"
> >>> print wraplines(s)
> spam
> ham
> >>>
>
> HTH
>
> --
> Arnaud
>
> --
> http://mail.python.org/mailman/listinfo/p...
>