[Tutor] Displaying data in columns

bob gailer bgailer at gmail.com
Mon May 7 16:21:07 CEST 2012


On 5/7/2012 9:43 AM, Thomas C. Hicks wrote:
> I have some data that comes out of a database as a list of tuples of
> integers and strings, one tuple for each row in the ResultProxy from
> the select operation. The data looks something like this:
Where the data comes from is not relevant. Please don't clutter your 
questions with irrelevant facts.
> [(56, 12, 8, u'2012-02', 10, 12, u'Guangxi Province', u'Guangxi',
> u'10', 8, u'TOT'), (57, 21, 1, u'2012-03', 36, 21,
> u'Sichuan EQ Region', u'Sichuan', u'2', 1, u'Basic Medical -
> Rural')]
>
> I would like to display some of the items in the tuples in columnar
> format with each column lining up on the left side (below should be
> three neat columns with location, training type, number trained and
> date):
>
> Guangxi Province        TOT			10	2012-02
> Sichuan EQ Region	Basic Medical - Rural	36	2012-03
>
> Surely there is a python module that helps with that, isn't there?  I
> know I could write a function that reads the items, figures string
> lengths, adds padding white space and prints out the columns but am
> hoping there is a more elegant solution.  I am not against reading
> documentation, just can't find the right module to read about.
the basic tool you'd use is string formatting. I prefer the original %.

"%24s%24s%8s%7s" % <tuple-of-data>

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list