text wrapping help

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

    #1

    text wrapping help

    I'm trying to text wrap a string but not using the textwrap module. I
    have 24x9 "matrix" and the string needs to be text wrapped according
    to those dimensions. Is there a known algorithm for this? Maybe some
    kind of regular expression? I'm having difficulty programming the
    algorithm. Thanks,
    Ryan

  • Ryan K

    #2
    Re: text wrapping help

    Okay, I was a little vague in my last post...the matrix is like a
    Wheel of Fortune board and it knows nothing about newlines. After 24
    characters it spills over onto the next line. Here is what I came up
    with:

    <pre>
    ## Wrap Text

    def wrap_text(in_st r, chars_line):
    """ wrap text """
    spaces = 0
    new_str = ""
    tmp_str = ""
    while True:
    tmp_str = in_str[:chars_line]
    if tmp_str[chars_line-1].isspace():
    new_str += tmp_str
    else:
    spaces = 0
    for ch in reversed(tmp_st r):
    if ch.isspace():
    break
    spaces += 1
    tmp_str = tmp_str[:chars_line-spaces]
    tmp_str += " " * spaces
    new_str += tmp_str
    in_str = in_str[chars_line-spaces:]
    if len(in_str) <= chars_line:
    new_str += " " + in_str
    break
    return new_str


    if __name__ == '__main__':

    sample_text = """Personal firewall software may warn about the
    connection IDLE makes to its subprocess using this computer's internal
    loopback interface. This connection is not visible on any external
    interface and no data is sent to or received from the Internet."""
    print wrap_text(sampl e_text, 24)

    </pre>

    It doesn't even run but when I go through it interactively it seems
    okay. Once again, any help is appreciated.


    On Feb 28, 7:06 pm, "Ryan K" <ryankas...@gma il.comwrote:
    I'm trying to text wrap a string but not using the textwrap module. I
    have 24x9 "matrix" and the string needs to be text wrapped according
    to those dimensions. Is there a known algorithm for this? Maybe some
    kind of regular expression? I'm having difficulty programming the
    algorithm. Thanks,
    Ryan

    Comment

    • attn.steven.kuo@gmail.com

      #3
      Re: text wrapping help

      On Feb 28, 4:06 pm, "Ryan K" <ryankas...@gma il.comwrote:
      I'm trying to text wrap a string but not using the textwrap module. I
      have 24x9 "matrix" and the string needs to be text wrapped according
      to those dimensions. Is there a known algorithm for this? Maybe some
      kind of regular expression? I'm having difficulty programming the
      algorithm.

      Try:

      import re
      sample_text = """Personal firewall software may warn about the
      connection IDLE makes to its subprocess using this computer's internal
      loopback interface. This connection is not visible on any external
      interface and no data is sent to or received from the Internet."""

      # assume 24 is sufficiently wide:

      lines = map(lambda x: x.rstrip(),
      re.findall(r'.{ 1,24}(?:(?<=\S) \s|$)', sample_text.rep lace("\n", " ")))

      print "\n".join(lines )


      --
      Hope this helps,
      Steven

      Comment

      • Ryan K

        #4
        Re: text wrapping help

        That works great but I need to replace the newlines with 24-(the index
        of the \n) spaces.



        On Feb 28, 8:27 pm, attn.steven.... @gmail.com wrote:
        On Feb 28, 4:06 pm, "Ryan K" <ryankas...@gma il.comwrote:
        >
        I'm trying to text wrap a string but not using the textwrap module. I
        have 24x9 "matrix" and the string needs to be text wrapped according
        to those dimensions. Is there a known algorithm for this? Maybe some
        kind of regular expression? I'm having difficulty programming the
        algorithm.
        >
        Try:
        >
        import re
        sample_text = """Personal firewall software may warn about the
        connection IDLE makes to its subprocess using this computer's internal
        loopback interface. This connection is not visible on any external
        interface and no data is sent to or received from the Internet."""
        >
        # assume 24 is sufficiently wide:
        >
        lines = map(lambda x: x.rstrip(),
        re.findall(r'.{ 1,24}(?:(?<=\S) \s|$)', sample_text.rep lace("\n", " ")))
        >
        print "\n".join(lines )
        >
        --
        Hope this helps,
        Steven

        Comment

        • attn.steven.kuo@gmail.com

          #5
          Re: text wrapping help

          On Feb 28, 5:50 pm, "Ryan K" <ryankas...@gma il.comwrote:
          On Feb 28, 8:27 pm, attn.steven.... @gmail.com wrote:
          >
          Try:
          >
          import re
          sample_text = """Personal firewall software may warn about the
          connection IDLE makes to its subprocess using this computer's internal
          loopback interface. This connection is not visible on any external
          interface and no data is sent to or received from the Internet."""
          >
          # assume 24 is sufficiently wide:
          >
          lines = map(lambda x: x.rstrip(),
          re.findall(r'.{ 1,24}(?:(?<=\S) \s|$)', sample_text.rep lace("\n", " ")))
          >
          print "\n".join(lines )
          >
          --
          Hope this helps,
          Steven
          >
          That works great but I need to replace the newlines with 24-(the index
          of the \n) spaces.
          >


          Just left-justify to the appropriate width with
          the the padding character you wanted:

          equally_long_li nes = map(lambda x: x.ljust(24, ' '), lines)

          print "\n".join(equal ly_long_lines)

          --
          Hope this helps,
          Steven

          Comment

          • Bjoern Schliessmann

            #6
            Re: text wrapping help

            Ryan K wrote:
            It doesn't even run but when I go through it interactively it
            seems okay. Once again, any help is appreciated.
            I haven't tested yet, but why don't you make a list of words of the
            text (with split), and then accumulate words in a list until the
            next word would make the line too long. Then just join the word
            list, fill with spaces until line end and start over.

            Regards,


            Björn

            --
            BOFH excuse #325:

            Your processor does not develop enough heat.

            Comment

            Working...