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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanjay

    #1

    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

  • Boris Borcic

    #2
    Re: Routine for prefixing '>' before every line of a string

    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(lin e_separator) :
    for chunkstart in range(0,len(lin e),maxsize) :
    yield prefix + line[chunkstart:chun kstart+maxsize]

    Comment

    • Peter Otten

      #3
      Re: Routine for prefixing '>' before every line of a string

      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.TextWr apper(20, initial_indent= "] ",
      subsequent_inde nt="] ").fill
      >>print format("alpha beta gamma delta\nepsilon zeta eta theta")
      ] alpha beta gamma
      ] delta epsilon zeta
      ] eta theta

      Peter

      Comment

      • Roberto Bonvallet

        #4
        Re: Routine for prefixing '>' before every line of a string

        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:
        Source code: Lib/textwrap.py The textwrap module provides some convenience functions, as well as TextWrapper, the class that does all the work. If you’re just wrapping or filling one or two text st...


        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(t ext, 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

        Comment

        • Neil Cerutti

          #5
          Re: Routine for prefixing '>' before every line of a string

          On 2006-12-14, Roberto Bonvallet <Roberto.Bonval let@cern.chwrot e:
          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:
          Source code: Lib/textwrap.py The textwrap module provides some convenience functions, as well as TextWrapper, the class that does all the work. If you’re just wrapping or filling one or two text st...

          >
          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(t ext, 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

          Comment

          • Sanjay

            #6
            Re: Routine for prefixing '&gt;' before every line of a string

            Thanks a lot, guys!

            sanjay

            Comment

            Working...