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

S

Sanjay

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
 
B

Boris Borcic

Sanjay said:
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]
 
P

Peter Otten

Sanjay said:
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
 
R

Roberto Bonvallet

Sanjay said:
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,
 
N

Neil Cerutti

Sanjay said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top