pptable, named using the same fashion as pprint, is a library for pretty printing list of dictionaries as plain text table.
Consider the following example code:
from pptable import pptable
data = [
{'slug': u'ams1', 'name': u'Amsterdam 1', 'region_id': 2},
{'slug': u'sfo1', 'name': u'San Francisco 1', 'region_id': 3},
{'slug': u'nyc2', 'name': u'New York 2', 'region_id': 4},
{'slug': u'ams2', 'name': u'Amsterdam 2', 'region_id': 5},
{'slug': u'sgp1', 'name': u'Singapore 1', 'region_id': 6}]
pptable(data)
The input data is a list of dict, pprint.pprint renders the data as:
----------------------------------
region_id name slug
----------------------------------
2 Amsterdam 1 ams1
3 San Francisco 1 sfo1
4 New York 2 nyc2
5 Amsterdam 2 ams2
6 Singapore 1 sgp1
You could pick the columns and set the order of them:
headers = ['slug', 'name']
pptable(data, headers=headers)
In the example above, with only keys slug and name, in that order, the output table would be:
-----------------------
slug name
-----------------------
ams1 Amsterdam 1
sfo1 San Francisco 1
nyc2 New York 2
ams2 Amsterdam 2
sgp1 Singapore 1
Just a day ago, a similar library plaintable was made, not sure why two coincidentally showed up around the same time. Nonetheless, I know they wouldn’t be only two.
pptable is written by Mateusz Lapsa-Malawski under the MIT License, currently version 0.0.2 (2014-04-30), only works for Python 2 at this moment.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.