Formatting Output

Chris cwitts at gmail.com
Mon Jun 2 17:40:25 EDT 2008


On Jun 2, 11:34 pm, Chris <cwi... at gmail.com> wrote:
> On Jun 2, 9:43 pm, Doug Morse <mo... at edoug.org> wrote:
>
>
>
> > On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <mensana... at aol.com> wrote:
> > >  On Jun 2, 3:38 am, Chris <cwi... at gmail.com> wrote:
> > > > On Jun 2, 9:34 am, "victor.hera... at gmail.com"
>
> > > > <victor.hera... at gmail.com> wrote:
> > > > > Hi,
>
> > > > > i am building a little script and i want to output a series of columns
> > > > > more or less like this:
>
> > > > > 1  5  6
> > > > > 2  2  8
> > > > > 2  9  5
> > > ...
>
> > I have a related question:
>
> > Does Python have (or can emulate) the formatted output capability found in
> > Perl?
>
> > For example, all I have to do to get nicely formatted (i.e., aligned) output
> > is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT,
> > STDOUT_BOTTOM, etc.), exemplified by:
>
> >   format STDOUT_TOP =
> >   ------------------------------------------------------------------------------
> >   ~
> >   .
>
> >   format STDOUT =
> >   @<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<
> >   $res->{'full_name'},  $res->{'phone_1'},         $res->{'phone_1_type'}
> >   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~
> >   $res->{'address_1a'},                $res->{'address_2a'}
> >   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~
> >   $res->{'address_1b'},                $res->{'address_2b'}
> >   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~
> >   $res->{'address_1c'},                $res->{'address_2c'}
> >   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~
> >   $city_1                              $city_2
> >   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~
> >   $res->{'email_1'},                   $res->{'email_2'}
> >   ------------------------------------------------------------------------------
> >   ~
> >   .
>
> > Then, all I have to do is populate my $res object/hash as desired -- in this
> > example simple the results of a SQL query -- and lastly just call the "write"
> > function:
>
> >   write;
>
> > and Perl will produce very nicely formatted results.  This is useful not only
> > for producing human readable output, but also fixed-column-width data files,
> > etc.  I'd love to learn the Pythonistic way of doing the same thing.
>
> > Thanks!
> > Doug
>
> Can't seem to do this with dictionaries but...
>
> preformatted_string = """
> %s %20s %20s
> %s %30s
> %s %30s
> """
>
> print preformatted_string % ('first name'[:20], 'contact num 1'[:20],
>         'contact num type'[:20], 'address line 1'[:30], 'address line
> 2'[:30]
>         'address line 3'[:30], 'address line 4'[:30])
>
> You could do something like that.  the "[:20]" etc @ the end of the
> inputs is ofc to trim the strings to a max length.  The string
> formatter supports "%<number of characters to move to the right>s" so
> you can use that for alignment.  It's a bit late so maybe I buggered
> up when I tried to use dictionary assignment with it, but who knows :p

Actually just realised I had the number on the wrong side... :D

preformatted_string = """
%(first_name)s %(contact_num)20s %(contact_type)20s
"""
print preformatted_string % {'first_name':'Chris',
'contact_num':'555-5555', 'contact_type':'Home'}



More information about the Python-list mailing list