By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

TextWrapper keepking line breaks?

P: n/a
js
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.
Mar 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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(textwrap.fill(line) for line in
text.split('\n'))
...
>>s = "spam\nham"
print wraplines(s)
spam
ham
>>>
HTH

--
Arnaud

Mar 11 '08 #2

P: n/a
js
Hi Arnaud,

Great. Thanks for your help!

On Tue, Mar 11, 2008 at 10:27 AM, Arnaud Delobelle
<ar*****@googlemail.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(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
Mar 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.