Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 13th, 2008, 12:35 PM
elukkien@cmbi.ru.nl
Guest
 
Posts: n/a
Default 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:
Quote:
Quote:
Quote:
>>>str = "HelloWorld!"
.... {do something to str to set width to 2} ...
Quote:
Quote:
Quote:
>>>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

  #2  
Old August 13th, 2008, 12:55 PM
Duncan Booth
Guest
 
Posts: n/a
Default Re: format string to certain line width

elukkien@cmbi.ru.nl wrote:
Quote:
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:
>
Quote:
Quote:
>>>>str = "HelloWorld!"
>
... {do something to str to set width to 2} ...
>
Quote:
Quote:
>>>>print str
He
LL
oW
or
ld
!
Quote:
Quote:
Quote:
>>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
  #3  
Old August 13th, 2008, 01:15 PM
elukkien@cmbi.ru.nl
Guest
 
Posts: n/a
Default Re: format string to certain line width

>>>import textwrap
Quote:
Quote:
Quote:
>>>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.

  #4  
Old August 13th, 2008, 01:45 PM
Fredrik Lundh
Guest
 
Posts: n/a
Default Re: format string to certain line width

elukkien@cmbi.ru.nl wrote:
Quote:
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.findall('..', my_string))

or use writelines directly:

sys.stdout.writelines(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>

  #5  
Old August 13th, 2008, 01:55 PM
John Machin
Guest
 
Posts: n/a
Default Re: format string to certain line width

On Aug 13, 10:13 pm, elukk...@cmbi.ru.nl wrote:
Quote:
Quote:
Quote:
>>import textwrap
>>print textwrap.fill("HelloWorld", 2)
He
ll
oW
or
ld
>
Quote:
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.
Quote:
Quote:
Quote:
>>def julienne(s, w):
.... return ''.join(s[x:x+w] + '\n' for x in xrange(0, len(s), w))
....
Quote:
Quote:
Quote:
>>julienne('HelloWorld', 2)
'He\nll\noW\nor\nld\n'
Quote:
Quote:
Quote:
>>julienne('HelloWorld', 3)
'Hel\nloW\norl\nd\n'
Quote:
Quote:
Quote:
>>julienne('HelloWorld', 99)
'HelloWorld\n'
Quote:
Quote:
Quote:
>>>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles