473,385 Members | 1,890 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,385 software developers and data experts.

Routine for prefixing '>' before every line of a string

Hi All,

Is somewhere a routine useful to convert a string to lines of maxsize,
each prefixed with a '>'. This is a typical requirement for 'keeping
existing text while replying to a post in a forum'.

It can be developed, but if something obvious is already there(I being
new to python might not be aware), than it would be great just to reuse
it!

thanks
sanjay

Dec 14 '06 #1
5 1299
Sanjay wrote:
Hi All,

Is somewhere a routine useful to convert a string to lines of maxsize,
each prefixed with a '>'. This is a typical requirement for 'keeping
existing text while replying to a post in a forum'.

It can be developed, but if something obvious is already there(I being
new to python might not be aware), than it would be great just to reuse
it!

thanks
sanjay
def fixlines(stuff) :
for line in stuff.split(line_separator) :
for chunkstart in range(0,len(line),maxsize) :
yield prefix + line[chunkstart:chunkstart+maxsize]
Dec 14 '06 #2
Sanjay wrote:
Is somewhere a routine useful to convert a string to lines of maxsize,
each prefixed with a '>'. This is a typical requirement for 'keeping
existing text while replying to a post in a forum'.
>>import textwrap
format = textwrap.TextWrapper(20, initial_indent="] ",
subsequent_indent="] ").fill
>>print format("alpha beta gamma delta\nepsilon zeta eta theta")
] alpha beta gamma
] delta epsilon zeta
] eta theta

Peter

Dec 14 '06 #3
Sanjay wrote:
Is somewhere a routine useful to convert a string to lines of maxsize,
each prefixed with a '>'. This is a typical requirement for 'keeping
existing text while replying to a post in a forum'.
Take a look to the textwrap module:
http://docs.python.org/lib/module-textwrap.html

Here is an example:

# the text is actually a very long line
text = '''Lorem ipsum dolor sit amet, consectetuer adipiscing [...]'''
prefix = '>'

import textwrap
lines = ["%s %s" % (prefix, line) for line in textwrap.wrap(text, width=75)]

for line in lines:
print line

This prints:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam rhoncus,
justo eget facilisis gravida, lorem elit pellentesque urna, sed imperdiet
orci nisl sed nibh. Curabitur dignissim pretium magna. Proin nunc justo,
luctus ut, mollis sed, bibendum vel, nibh. Morbi rutrum est in nisl. Fusce
sagittis. Integer varius. Vivamus dapibus lectus sed nisl. Phasellus
gravida dignissim augue. Curabitur eget orci. Nulla ante augue, adipiscing
a, consequat ut, elementum ac, libero. Donec malesuada lacus vel quam. Ut a
massa vel velit fringilla rutrum. Maecenas massa sem, vulputate non,
lacinia eu, cursus ut, urna. Donec ultrices sollicitudin nunc. Sed vel arcu
in lacus posuere faucibus. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.
HTH. Cheers,
--
Roberto Bonvallet
Dec 14 '06 #4
On 2006-12-14, Roberto Bonvallet <Ro***************@cern.chwrote:
Sanjay wrote:
>Is somewhere a routine useful to convert a string to lines of
maxsize, each prefixed with a '>'. This is a typical
requirement for 'keeping existing text while replying to a
post in a forum'.

Take a look to the textwrap module:
http://docs.python.org/lib/module-textwrap.html

Here is an example:

# the text is actually a very long line
text = '''Lorem ipsum dolor sit amet, consectetuer adipiscing [...]'''
prefix = '>'

import textwrap
lines = ["%s %s" % (prefix, line) for line in textwrap.wrap(text, width=75)]
The solution will need to be instrumented in case of text that is
already quotes to one level. All in all, I recommend using Vim's
gq command or Emacs' autofill mode, which arlready do the right
thing.

--
Neil Cerutti
The Rev. Merriwether spoke briefly, much to the delight of the audience.
--Church Bulletin Blooper
Dec 14 '06 #5
Thanks a lot, guys!

sanjay

Dec 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Grumble | last post by:
Hello all, I want to read lines from a text file, where each line has the following syntax: token1:token2:token3 There could be white space between tokens and ':'
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
5
by: dharmdeep | last post by:
Hi friends, I had implemented client server application in C under VC++ environment. It can handle more than one client. So in this program i want to keep track of clients IP which gets connected...
7
by: Bob Darlington | last post by:
I'm using the following routine to call UpdateDiary() - below: Private Sub Form_BeforeUpdate(Cancel As Integer) On Error GoTo Form_BeforeUpdate_Error Call UpdateDiary(Me!TenantCounter,...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
16
by: Chuck | last post by:
Please help me correct the statements in sub "BoundData" The following sub is in a module. Everything runs with no error messages, however,data is not reaching the text box. Data is being...
4
by: dragony2000 | last post by:
I want to solve these questions using C# , Please !!! ************************************************************* 1- The factorial method is used frequently in probability problems. The...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
3
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.