473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Aug 13 '08 #1
4 1997
el******@cmbi.ru.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
Aug 13 '08 #2
>>>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.

Aug 13 '08 #3
el******@cmbi.ru.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.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>

Aug 13 '08 #4
On Aug 13, 10:13 pm, elukk...@cmbi.ru.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('HelloWorld', 2)
'He\nll\noW\nor\nld\n'
>>julienne('HelloWorld', 3)
'Hel\nloW\norl\nd\n'
>>julienne('HelloWorld', 99)
'HelloWorld\n'
>>>
Aug 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: RCS | last post by:
Okay, a rather 'interesting' situation has arisen at a place I work: I need to convert a database from Access to something that can be used over the web. I am currently maintaining and...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
0
by: lianfe_ravago | last post by:
Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the...
8
by: bienwell | last post by:
Hi, I have a problem of displaying data bound by a datalist control. In my table, I have a field Start_date which has Short Date data type. I tried to update this field by Current Date. After...
4
by: MC | last post by:
In C, printf allows us to specify a runtime (non-constant) field width of a formatted value by using the * character. Is there something like that for System.String.Format? In C#, I find myself...
2
by: tstephan | last post by:
In the docs for String.Format there is an alignment component which allows you to pad the field to a certain number of spaces. e.g. FormatFName = String.Format("First Name = |{0,10}|", myFName);...
6
by: gokhalen | last post by:
How can I print between column number 8 and column number 80?. Is there a format specifier that will do it easily?. I am printing an array of doubles and I want the number to be printed on the...
8
by: joemacbusiness | last post by:
Hi All, How do I format printed data in python? I could not find this in the Python Reference Manual: http://docs.python.org/ref/print.html Nor could I find it in Matloff's great tutorial:...
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, It may sound esoteric, but actually it is. Let's say there is a string resource in a project, the value is "AA BB". How can I only format the "BB" part into bold type. Can I finish the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.