TextWrapper keepking line breaks?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • js

    TextWrapper keepking line breaks?

    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.
  • Arnaud Delobelle

    #2
    Re: TextWrapper keepking line breaks?

    On Mar 10, 11:31 pm, js <ebgs...@gmail. comwrote:
    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(textw rap.fill(line) for line in
    text.split('\n' ))
    ...
    >>s = "spam\nham"
    >>print wraplines(s)
    spam
    ham
    >>>
    HTH

    --
    Arnaud

    Comment

    • js

      #3
      Re: TextWrapper keepking line breaks?

      Hi Arnaud,

      Great. Thanks for your help!

      On Tue, Mar 11, 2008 at 10:27 AM, Arnaud Delobelle
      <arnodel@google mail.comwrote:
      >
      On Mar 10, 11:31 pm, js <ebgs...@gmail. comwrote:
      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(textw rap.fill(line) for line in
      text.split('\n' ))
      ...
      >>s = "spam\nham"
      >>print wraplines(s)
      spam
      ham
      >>>
      >
      HTH
      >
      --
      Arnaud
      >
      --

      >

      Comment

      Working...