473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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',
Nov 7 '08 #1
1 3124
On Nov 7, 9:45*am, rh0dium <steven.kl...@gmail.comwrote:
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[i])
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
Nov 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
2
by: nephish | last post by:
Hey there, i have been spending some time learning python. i do enjoy it so. Heres the deal. i need some dirrection advice. i have a friend that wants me to set up a customer database for him....
4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
6
by: Rex Eastbourne | last post by:
Hi all, I've written the following simple macro called debug(aname, avalue) that prints out the name of an expression and its value: def debug(aname, avalue): print aname, 'is':...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
9
by: John Salerno | last post by:
Just wondering if this will ever happen, maybe in 3.0 when print becomes a function too? It would be a nice option to have it available without importing it every time, but maybe making it a...
2
by: Sheldon | last post by:
Good day, I would like to know if there is a way to print all the variables set in a python program with having to write "print variable" on all? sincerely, Sheldon
1
by: epilogue | last post by:
Hey guys Im pretty new to Java and while I am finding it enjoyable i am getting several errors!!! Do you think you could help me on this particular question of an Assignment im doing. I have to...
8
by: joemacbusiness | last post by:
Hi All, How do I format printed data in python? I could not find this in the Python Reference Manual: http://docs.python.org/ref/print.html Nor could I find it in Matloff's great tutorial:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.