[prog] Adding Column Headings in Text File

Almut Behrens almut-behrens at gmx.net
Sat Jul 2 08:09:28 EST 2005


On Fri, Jul 01, 2005 at 02:10:43AM -0400, Don Parris wrote:
> (...)
> The sample appears to use 2 tuples, "labels" & "data".  The last "print
> indent()" statement takes [labels]+rows as an argument.  Trying that in my
> code below presents a problem.  "heading" is a tuple, as is "Results"
> (coming from the MySQL query).  
> 
> ### My Code ###
>     # iterate through resultset.
>     heading = 'First Name', 'Last Name', 'Home Phone'
>     print 'Phone List'
>     print '%-15s\t%-15s\t%-15s' % heading
>     for record in Results:
>         print '%-15s\t%-15s\t%-15s' % record
>     hdg_List = list(heading)           # attempt to convert to list
>     mbrPhone = open('mbrPhone.txt', 'w')
>     mbrPhone.write(indent([heading]+Results, hasHeader=True,
>         	separateRows=False, prefix='', postfix='', justify='left',  
>                 wrapfunc=lambda x:str(x)))    mbrPhone.close()
> 
> O.k., I first display the results (along with headings) on the console. 
> Then I put it in a file for later use (e-mail to someone or print it). 
> Without the [heading]+ bit, the function works like a charm.  However,
> running as it is above (without the hdg_List statement), I get this
> traceback.
> 
>   (...)
>   File "/home/donp/python/ekklesia/ekklesia_db.py", line 65, in mbr_Phone
>     mbrPhone.write(indent([heading]+Results, hasHeader=True, separateRows=False,
    TypeError: can only concatenate list (not "tuple") to list
> 
> The function appears to be treating Results as a list, when it is supposed
> to be a tuple (or so I thought).

Not quite... :)  It's kind of the other way round:  Results would need
to be a list to be concat'able with [heading], which has become a list
by putting the square brackets around it...
Contatenation only works with equal types, so you could either write

      mbrPhone.write(indent( [heading] + list(Results),  ...
or
      mbrPhone.write(indent( (heading,) + Results,  ...

The former is concatenating two lists-of-tuples ('[heading]' is a list
with one tuple), while the latter is concatenating two tuples-of-tuples
(you need that weird '(heading,)' to make a tuple-of-tuples from one
tuple...).  

Just to elaborate a little:

Starting with the following definitions

  heading = 'First Name', 'Last Name', 'Home Phone'
  row1 = 'Row1 Col1', 'Row1 Col2', 'Row1 Col3'
  row2 = 'Row2 Col1', 'Row2 Col2', 'Row2 Col3'
  row3 = 'Row3 Col1', 'Row3 Col2', 'Row3 Col3'

the first parameter to the function indent() (which needs to be
sequence-of-sequences) could be one of the following:
(let's simply call that parameter 'data' here)

  data = heading, row1, row2, row3    # tuple-of-tuples

Or, with Results being
  Results = row1, row2, row3          # tuple-of-tuples
                                      # (that's what you have)
you could concatenate
  data = [heading] + list(Results)    # --> list-of-tuples
  data = (heading,) + Results         # --> tuple-of-tuples

Or, with Results being
  Results = [row1, row2, row3]        # list-of-tuples

you could concatenate
  data = [heading] + Results          # --> list-of-tuples
  data = (heading,) + tuple(Results)  # --> tuple-of-tuples


Both "list-of-tuples" and "tuple-of-tuples" qualify as
"sequence-of-sequences" -- with sequence being the more general term
for both list (mutable) and tuple (immutable).

(Thus, theoretically, "list-of-lists" and "tuple-of-lists" would also
qualify (among others), but as you already have tuples for the inner
sequences, we've just omitted all those combinations, for brevity... ;)

Does that make sense?

Almut



More information about the Programming mailing list