format string to certain line width

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • elukkien@cmbi.ru.nl

    format string to certain line width

    Hello!

    I'm trying to find a way to print out a long string (>400 characters no
    spaces, tabs or newlines) to a line width of 60 characters. So after every
    60 characters a newline would start. Is it possible to transform the
    string to set the linewidth?

    for example for a linewidth of 2:
    >>>str = "HelloWorld !"
    .... {do something to str to set width to 2} ...
    >>>print str
    He
    LL
    oW
    or
    ld
    !

    i know in Perl this works:
    my $str = "HelloWorld !" ;
    $str =~ s/.{2}/$&\n/g ; #at {2} you can then specify the desired width
    print $ str ;

    But how to do it in Python?
    An option in the print statement would also be fine.
    Something like: print "%{2}s" %(str)

    Of course i could do it in a for loop and print 60 characters, print a \n,
    print the next 60 characters, print a \n ...
    But i imagine there will be a more efficient way.

    Thanks
    Eddie

  • Duncan Booth

    #2
    Re: format string to certain line width

    elukkien@cmbi.r u.nl wrote:
    I'm trying to find a way to print out a long string (>400 characters
    no spaces, tabs or newlines) to a line width of 60 characters. So
    after every 60 characters a newline would start. Is it possible to
    transform the string to set the linewidth?
    >
    for example for a linewidth of 2:
    >
    >>>>str = "HelloWorld !"
    >
    ... {do something to str to set width to 2} ...
    >
    >>>>print str
    He
    LL
    oW
    or
    ld
    !
    >>import textwrap
    >>print textwrap.fill(" HelloWorld", 2)
    He
    ll
    oW
    or
    ld

    Of course if your assertion that the string contains no spaces, tabs or
    newlines turns out to be incorrect this may not do what you wanted.

    --
    Duncan Booth http://kupuguy.blogspot.com

    Comment

    • elukkien@cmbi.ru.nl

      #3
      Re: format string to certain line width

      >>>import textwrap
      >>>print textwrap.fill(" HelloWorld", 2)
      He
      ll
      oW
      or
      ld
      >
      Of course if your assertion that the string contains no spaces, tabs or
      newlines turns out to be incorrect this may not do what you wanted.
      Thanks, i just found this myself and it works fine, but very slow...
      The script without the wrapping takes 30 seconds, with wrapping 30
      minutes. Is there not a more efficient way?
      The perl syntax i posted is much faster.

      Comment

      • Fredrik Lundh

        #4
        Re: format string to certain line width

        elukkien@cmbi.r u.nl wrote:
        Thanks, i just found this myself and it works fine, but very slow...
        The script without the wrapping takes 30 seconds, with wrapping 30
        minutes. Is there not a more efficient way?
        sounds like you're wrapping a few million long strings, not just one...

        here are two approaches that are about 10x faster than textwrap:

        re.findall('..' , my_string)

        or

        [my_string[i:i+2] for i in xrange(0, len(my_string), 2]

        the above gives you lists of string fragments; you can either join them
        before printing; e.g.

        print "'\n'.join(re.f indall('..', my_string))

        or use writelines directly:

        sys.stdout.writ elines(s + "\n" for s in re.findall('..' , my_string))

        Python's re.sub isn't as efficient as Perl's corresponding function, so
        a direct translation of your Perl code to

        re.sub('(..)', r'\1\n', my_string)

        or, faster (!):

        re.sub('(..)', lambda m: m.group() + "\n", my_string)

        is not as fast as the above solutions.

        </F>

        Comment

        • John Machin

          #5
          Re: format string to certain line width

          On Aug 13, 10:13 pm, elukk...@cmbi.r u.nl wrote:
          >>import textwrap
          >>print textwrap.fill(" HelloWorld", 2)
          He
          ll
          oW
          or
          ld
          >
          Of course if your assertion that the string contains no spaces, tabs or
          newlines turns out to be incorrect this may not do what you wanted.
          >
          Thanks, i just found this myself and it works fine, but very slow...
          The script without the wrapping takes 30 seconds, with wrapping 30
          minutes. Is there not a more efficient way?
          The perl syntax i posted is much faster.
          >>def julienne(s, w):
          .... return ''.join(s[x:x+w] + '\n' for x in xrange(0, len(s), w))
          ....
          >>julienne('Hel loWorld', 2)
          'He\nll\noW\nor \nld\n'
          >>julienne('Hel loWorld', 3)
          'Hel\nloW\norl\ nd\n'
          >>julienne('Hel loWorld', 99)
          'HelloWorld\n'
          >>>

          Comment

          Working...