Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool in Python to create text tables like Powershell?

Tags:

python

console

Whenever PowerShell displays its objects, it formats them in a nice pretty manner in the monospaced console, e.g.:

> ps

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    101       5     1284       3656    32     0.03   3876 alg
    257       7     4856      10228    69     0.67    872 asghost
    101       4     3080       4696    38     0.36   1744 atiptaxx
    179       7     5568       7008    54     0.22    716 BTSTAC~1
...

Is there a similar library in Python or do I get to make one?

like image 974
Nick T Avatar asked Nov 28 '10 04:11

Nick T


2 Answers

I think python texttable module does exactly what you were looking for.

For further information.

like image 161
systemsfault Avatar answered Sep 16 '22 15:09

systemsfault


pprint — Data pretty printer

There is a pprint module which does a little bit of that. It's not as nice as your example but it does at least try to print 2-D and recursive data structures more intelligently than str().

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets, classes, or instances are included, as well as many other built-in objects which are not representable as Python constants.

numpy — Scientific computing

Then there is numpy module which is targeted at mathematic and scientific computing, but is also pretty darn good at displaying matrices:

>>> from numpy import arange
>>> b = arange(12).reshape(4,3)  # 2d array
>>> print b
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
like image 42
John Kugelman Avatar answered Sep 19 '22 15:09

John Kugelman