[Tutor] formatting sql output
Ivan Van Laningham
ivanlan@callware.com
Mon, 03 May 1999 16:13:52 -0600
Hi All--
Stephanie Jones wrote:
>
> I am currently trying to format the output of a sql query using the
> techniques described in the book "Internet Programming with Python" under
> the subsection "Building an ASCII Table Generator" (pp 65-66).
> Basically, the result of the query is a tuple called "opened". I then have,
> following the example in the book:
>
> for x in opened:
> list=[x[0],x[2],x[3],x[1]]
> printstring=formatfields(list)
> print printstring
>
> def formatfields(list):
> fieldsformatted=map(fieldformat15,list)
> return string.joinfields(fieldsformatted, " | ")
>
> def fieldformat15(field):
> return '%-15s' % field
>
> However, it doesn't seem to be formatting so that each "field" is 15
> characters long. Does my question make sense, and does anyone have an idea
> why it is doing this?
>
The format specification is incorrect. You should use "%15.15s" or
"%-15.15s" to force left justification, in addition to padding/clipping.
<procrustean>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------