Routine for prefixing '>' before every line of a string
Neil Cerutti
horpner at yahoo.com
Thu Dec 14 08:55:09 EST 2006
On 2006-12-14, Roberto Bonvallet <Roberto.Bonvallet at cern.ch> wrote:
> 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
More information about the Python-list
mailing list