473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

TextWrapper keepking line breaks?

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
2 2121
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: dan glenn | last post by:
Hi. I'm finding that if I have text entered into a <textarea ...> </textarea> in a form, and that text has leading blank lines (with no spaces or anything else), that when I retrieve the entered...
1
by: Bill | last post by:
I am downloading data from a website that displays it in a table $fp = fopen("a website page", 'r'); The following accesses the stream one <td> element at a time $myData = fgets($fp); Then I...
6
by: Jonathan | last post by:
I want to save textarea contents to a mysql database with the paragraph breaks intact without having to type paragraph or break tags in HTML. How can I do that. So far, although it occurs naturally...
12
by: scott | last post by:
Is there a way to prevent IE from forcing a line break? Below in LISTING 2, you notice the table background image extention of '.jpg' begins a new line, instead of staying on the same line as it's...
4
by: intl04 | last post by:
I have a memo field that is included in some Access reports I created. Is there some way for the memo field to display nicely formatted text, with line breaks between paragraphs? Or is it necessary...
2
by: Mike | last post by:
I need my textbox to work more smoothly with respect to line breaks. When I have data pulled from the database into a textbox there are hard line breaks at the end of each line (by definition how...
5
by: joelbyrd | last post by:
Didn't know exactly where to post this, but: How do I get line breaks in a textarea? I'm pulling text from a database, and this text definately has line breaks in it, because I replaced all the...
2
by: caspardh | last post by:
i was trying to code a morse code decipherer which workd fine except that the .txt file that i was converting had linebreaks built in, i cant get python to ignore the line breaks and i cant find any...
1
by: =?ISO-8859-1?Q?thib=B4?= | last post by:
Hi, Taking a closer look to highlight_file() lastly, I found out that its line break policy was a bit.. strange. <?php # highlight_file() line breaks test $f = fopen('highlight_file_test',...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.