<html>
<body>
At 04:31 PM 5/6/2004, Lee Harr wrote:<br>
<blockquote type=cite class=cite cite><blockquote type=cite class=cite cite>I
wrote a little program to look for graphics files and tell me the
image<br>
size and the file size of each file. The data prints out like
this:<br><br>
(292, 240) 35638 defender.bmp<br>
(1024, 768) 2359350 evolution3.bmp<br>
(78, 76) 17990 GRID1A.bmp<br><br>
How can I make it so that it looks more like this:<br><br>
( 292,&nbsp; 240)&nbsp;&nbsp;&nbsp; 35,638&nbsp; defender.bmp<br>
(1024,&nbsp; 768) 2,359,350&nbsp; evolution3.bmp<br>
(&nbsp; 78,&nbsp;&nbsp; 76)&nbsp;&nbsp;&nbsp; 17,990&nbsp;
GRID1A.bmp<br>
</blockquote>Please post only in plain text.<br><br>
You will want to look at the string formatting operator:<br>
<a href="http://docs.python.org/lib/typesseq-strings.html" eudora="autourl">http://docs.python.org/lib/typesseq-strings.html</a><br>
<br>
One handy example:<br><br>
<blockquote type=cite class=cite cite><blockquote type=cite class=cite cite><blockquote type=cite class=cite cite>a='(292,
240)';b=35638; c='defender.bmp'<br>
print '%10s %10s %20s' % (a, b,
c)</blockquote></blockquote></blockquote>(292,
240)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
35638&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
defender.bmp</blockquote><br>
This solution is close, but I think the OP wants the numbers in () to be
individually aligned in columns. The desired output in proportional font
doesn't show this, but if you copy/paste into a fixed font editor you
will see this more clearly.<br><br>
So the challenge is to parse the numbers out and format them
individually. I think this is best done using re:<br><br>
import re<br>
m = re.match(r'\((\d+),\s*(\d+)\)', <font size=2>im.size) # generates a
match object with 2 groups<br>
</font># m.groups() is ('292', '240')<br>
'(%4s, %4s)' % m.groups() # is '( 292,&nbsp; 240)'<br><br>
re can be daunting at first but is well worth the learning curve. So
let''s break the regular expression \((\d+),\s*(\d+)\) into pieces:<br>
\(<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab># match a left
parentheses<br>
\s*<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab># match 0 or more
&quot;whitespace&quot; characters<br>
(\d+)<x-tab>&nbsp;&nbsp;&nbsp;</x-tab># match one or more digits and
place them in a group<br>
,<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab># match a
comma<br>
\s*<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab># match 0 or more
&quot;whitespace&quot; characters<br>
(\d+)<x-tab>&nbsp;&nbsp;&nbsp;</x-tab># match one or more digits and
place them in another group<br>
\)<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab># match a right
parenthesis<br>
The result is placed in a &quot;match object&quot;. m.groups() returns
all the groups in a tuple, which is the desired right argument type for
%<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625 home<br>
720 938 2625 cell</body>
</html>