Simple - Pretty Printing (pprint) a list with a max width?

R

rh0dium

Hi all,

Perhaps it's not supposed to work like this but I thought if you
supplied a width to pprint it would nicely format a list to the
width.

KEYS = ['search_keys', 'Section', 'site', 'Employee', 'JobClassCode',
'XBoss', 'Department',
'LocationDesc', 'cn', 'Division', 'Fax', 'SectionCode',
'last_update', 'location',
'mail', 'Nickname', 'JobClass', 'username', 'Name',
'Shift', 'FunctionalArea', 'p4user',
'euid', 'Position', 'DivisionCode', 'OfficePhone',
'CellPhone', 'Supervisor', 'aim',
'url', 'aim_id', 'Assistant', 'DeptNo', 'OtherPhone',
'LocationCode']

pp = pprint.PrettyPrinter(indent=2)
pp.pprint(KEYS)
[ 'search_keys',
'Section',
.
.
That works but it doesn't span the width if you put a width - for that
matter I can't seem to get width to do anything. Anyone else see
that? What I want is to be able to given a list provide a nice fancy
evenly spaced columnar output.

search_keys Section site Employee
JobClassCode XBoss Department LocationDesc
cn Division Fax SectionCode

It's clear pprint does not do this - Does anyone have something for
working with lists to pretty format them?
 
P

Paul McGuire

Hi all,

Perhaps it's not supposed to work like this but I thought if you
supplied a width to pprint it would nicely format a list to the
width.

KEYS = ['search_keys', 'Section', 'site', 'Employee', 'JobClassCode',
'XBoss', 'Department',
            'LocationDesc',  'cn', 'Division', 'Fax', 'SectionCode',
'last_update',  'location',
            'mail', 'Nickname', 'JobClass', 'username', 'Name',
'Shift', 'FunctionalArea', 'p4user',
            'euid', 'Position',  'DivisionCode', 'OfficePhone',
'CellPhone', 'Supervisor', 'aim',
            'url', 'aim_id', 'Assistant', 'DeptNo', 'OtherPhone',
'LocationCode']

 pp = pprint.PrettyPrinter(indent=2)
 pp.pprint(KEYS)
  [ 'search_keys',
    'Section',
  .
  .
That works but it doesn't span the width if you put a width - for that
matter I can't seem to get width to do anything.  Anyone else see
that?   What I want is to be able to given a list provide a nice fancy
evenly spaced columnar output.

  search_keys        Section        site          Employee
  JobClassCode       XBoss          Department    LocationDesc
  cn                 Division       Fax           SectionCode

It's clear pprint does not do this - Does anyone have something for
working with lists to pretty format them?

Try this:

data = [
"this is a really long entry, longer than will fit in a single
column",
"this is another one, boy this guy really talks a lot!",
"and in conclusion, I find that more is more, except when it's not"
]

import textwrap
from itertools import izip,chain,repeat

for linetuple in izip(map(None,*[textwrap.wrap(d,15)
for d in data])):
linetuple = ("" if c is None else c for c in linetuple[0])
print "|%-15s|%-15s|%-15s|" % tuple(linetuple)

print
colwidths = [16,15,12]
fmtstring = "|" + "|".join("%%-%ds" % w for w in colwidths) + "|"
for linetuple in izip(map(None,*[textwrap.wrap(d,colwidths)
for i,d in enumerate(data)])):
linetuple = ("" if c is None else c for c in linetuple[0])
print fmtstring % tuple(linetuple)

Prints:

|this is a |this is another|and in |
|really long |one, boy this |conclusion, I |
|entry, longer |guy really |find that more |
|than will fit |talks a lot! |is more, except|
|in a single | |when it's not |
|column | | |

|this is a really|this is another|and in |
|long entry, |one, boy this |conclusion, |
|longer than will|guy really |I find that |
|fit in a single |talks a lot! |more is |
|column | |more, except|
| | |when it's |
| | |not |

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top