Formatting Output

Doug Morse morse at edoug.org
Mon Jun 2 15:43:19 EDT 2008


On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator <mensanator 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



More information about the Python-list mailing list