[prog] Adding Column Headings in Text File
Don Parris
webdev at matheteuo.org
Fri Jul 1 16:10:43 EST 2005
Well, I've managed to get my queried data into a neatly formatted text file.
The problem now is that I really could use the column headings as well. I
tried going by the example given in the recipe, but have a problem with
concatenating the heading tuple to the Results tuple. I would get the
column names from the tables during the query process, but then the column
names would look like "hphn" instead of "Home Phone".
It would appear that the best approach is to present the column headings in
a manner that my current code can accept. Here's the sample from the recipe
(from the Python Cookbook site -
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662):
### Recipe Sample (ignore the funky line breaks) ###
# if __name__ == '__main__':
# labels = ('First Name', 'Last Name', 'Age', 'Position')
# data = \
# '''John,Smith,24,Software Engineer
# Mary,Brohowski,23,Sales Manager
# Aristidis,Papageorgopoulos,28,Senior Reseacher'''
# rows = [row.strip().split(',') for row in data.splitlines()]
#
# print 'Without wrapping function\n'
# print indent([labels]+rows, hasHeader=True)
# # test indent with different wrapping functions
# width = 10
# for wrapper in (wrap_always,wrap_onspace,wrap_onspace_strict):
# print 'Wrapping function: %s(x,width=%d)\n' %
# (wrapper.__name__,width)
# print indent([labels]+rows, hasHeader=True, separateRows=True,#
# prefix='| ', postfix=' |', wrapfunc=lambda x:wrapper(x,width))
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.
### Traceback ###
Traceback (most recent call last):
File "ekklesia.py", line 165, in ?
Main()
File "ekklesia.py", line 160, in Main
RunMenu(Menu_Main)
File "ekklesia.py", line 31, in RunMenu
if len(MenuList[sel]) == 3: MenuList[sel][1](MenuList[sel][2])
File "ekklesia.py", line 32, in RunMenu
else: MenuList[sel][1]()
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). I attempted to convert the heading to a
list with the hdg_List statement (per an example of converting a tuple to a
list), but with similar results:
Traceback (most recent call last):
File "ekklesia.py", line 165, in ?
Main()
File "ekklesia.py", line 160, in Main
RunMenu(Menu_Main)
File "ekklesia.py", line 31, in RunMenu
if len(MenuList[sel]) == 3: MenuList[sel][1](MenuList[sel][2])
File "ekklesia.py", line 32, in RunMenu
else: MenuList[sel][1]()
File "/home/donp/python/ekklesia/ekklesia_db.py", line 65, in mbr_Phone
mbrPhone.write(indent([hdg_List]+Results, hasHeader=True,
separateRows=False, TypeError: can only concatenate list (not "tuple") to
list
Perhaps I'm not thinking about the problem correctly. Actually, I can use
the input() function with the print statement as well, so that the user gets
something of a WYSIWYG result, where screen output == printer output.
Anyway, any ideas on solving this issue? Thanks!
Don
--
evangelinux GNU Evangelist
http://matheteuo.org/ http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime
anywhere."
More information about the Programming
mailing list