[prog] Adding Column Headings in Text File

Almut Behrens almut-behrens at gmx.net
Sat Jul 2 23:55:53 EST 2005


On Fri, Jul 01, 2005 at 11:38:39PM -0400, Don Parris wrote:
> 
> But why didn't indent() recognize heading (no brackets) as the tuple it was?
>  It saw Results, but not heading, as a tuple.  Results was not stated as
> (Results), but a simply Results. In my newbie way of thinking, indent()
> should have known that heading - not [heading] - was also a tuple.

indent() just gets a complex data structure, and renders it according
to its expectation of receiving a seq-of-seqs.  (BTW, why is that
routine called "indent" in the cookbook example? -- I would've thought
that "renderTable", "formatTable", or so, would be more appropriate...)

The problem is the different aggregation of 'heading' and 'Results'. 
So, depending on how you combine them into the single tuple/list
parameter that you pass to indent(), you'll get different results.
Most of the possible combinations are not what you want, though :)

Syntactically, it's no problem to concatenate both tuples as is, but
what you get is a six-element tuple (with the example definitions
from my previous mail, that is).  indent() in this case does render
six rows.  Also, as it expects each row to be a sequence of column
items, it simply splits up the individual title strings into single
characters... (strings are sequences, too -- seqs of characters).

Just play around with different ways of combining the heading with the
rest of the table, and print out the resulting data structures, using
Python's neat built-in feature to stringify complex structures into
nice, human-readable representations.  For example:

  Results = row1, row2, row3
  data = heading + Results        # not what you want
  
  print 'Representation:', data
  print
  print indent(data, ... ... )

This prints (output slightly reformatted):

Representation: ('First Name',
                 'Last Name',
                 'Home Phone',
                 ('Row1 Col1', 'Row1 Col2', 'Row1 Col3'),
                 ('Row2 Col1', 'Row2 Col2', 'Row2 Col3'),
                 ('Row3 Col1', 'Row3 Col2', 'Row3 Col3'))

F         | i         | r         | s    | t    |      | N    | a    | m    | e   
----------------------------------------------------------------------------------
L         | a         | s         | t    |      | N    | a    | m    | e   
H         | o         | m         | e    |      | P    | h    | o    | n    | e   
Row1 Col1 | Row1 Col2 | Row1 Col3
Row2 Col1 | Row2 Col2 | Row2 Col3
Row3 Col1 | Row3 Col2 | Row3 Col3


Similarly nonsensical results are achieved by combining heading and
table body by listing them next to each other, forming a two-element
tuple:

  data = heading, Results

Representation: (('First Name', 'Last Name', 'Home Phone'),
                 (('Row1 Col1', 'Row1 Col2', 'Row1 Col3'),
                  ('Row2 Col1', 'Row2 Col2', 'Row2 Col3'),
                  ('Row3 Col1', 'Row3 Col2', 'Row3 Col3')))

First Name                              | Last Name                               | Home Phone                             
---------------------------------------------------------------------------------------------------------------------------
('Row1 Col1', 'Row1 Col2', 'Row1 Col3') | ('Row2 Col1', 'Row2 Col2', 'Row2 Col3') | ('Row3 Col1', 'Row3 Col2', 'Row3 Col3')

This produces two rows, with cells of the second row being tuples, so
they are simply being auto-stringified...

In contrast, the correct representation looks like this:

  data = (heading,) + Results

Representation: (('First Name', 'Last Name', 'Home Phone'),
                 ('Row1 Col1', 'Row1 Col2', 'Row1 Col3'),
                 ('Row2 Col1', 'Row2 Col2', 'Row2 Col3'),
                 ('Row3 Col1', 'Row3 Col2', 'Row3 Col3'))

First Name | Last Name | Home Phone
-----------------------------------
Row1 Col1  | Row1 Col2 | Row1 Col3 
Row2 Col1  | Row2 Col2 | Row2 Col3 
Row3 Col1  | Row3 Col2 | Row3 Col3 

Well, you get the idea... :)


> ... In my op, I
> included the example from the recipe.  I thought the rows data was in the
> form of a tuple, but I suppose it's actually a list, since they called
> [labels], rather than (labels).

Exactly... 'rows' is being created like this

  rows = [ row.strip().split(',')  for row in data.splitlines() ]

Thus, it's a list.


> ..., but I am enjoying the learning experience.  I'm also
> beginning to feel more comfortable with the terminology and actually writing
> the code.  Despite the challenge this represents for me, I'm actually kind
> of enjoying writing code.  I never would have dreamed it.

That's good :)  Have fun!

Almut



More information about the Programming mailing list