TextWrapper keepking line breaks?

Discussion in 'Python' started by js, Mar 10, 2008.

  1. js

    js Guest

    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.
     
    js, Mar 10, 2008
    #1
    1. Advertising

  2. On Mar 10, 11:31 pm, js <> 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
     
    Arnaud Delobelle, Mar 11, 2008
    #2
    1. Advertising

  3. js

    js Guest

    Hi Arnaud,

    Great. Thanks for your help!

    On Tue, Mar 11, 2008 at 10:27 AM, Arnaud Delobelle
    <> wrote:
    >
    > On Mar 10, 11:31 pm, js <> 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/python-list
    >
     
    js, Mar 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raj Mudaliar
    Replies:
    0
    Views:
    2,291
    Raj Mudaliar
    Jul 14, 2003
  2. PJ
    Replies:
    1
    Views:
    526
    Sasha Shevelev
    Aug 4, 2003
  3. Andrew Banks

    Maintaining line breaks in HTML email

    Andrew Banks, Apr 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    900
    Andrew Banks
    Apr 4, 2004
  4. bernadou

    Force multi line field value to output with line breaks?

    bernadou, Jan 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    370
    bernadou
    Jan 23, 2006
  5. Marek Stepanek
    Replies:
    12
    Views:
    422
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page