Skip to main content

hprint

Print python object as table/json format in an easy way based on python-tabulate.

The main use cases of the library are:

  • table print list of records (dict)
  • customize table header name
  • customize missing value
  • automatically print json string when fail to print as table
  • support expanded output, like postgres

Installation

To install the Python library and the command line utility, run:

pip install -U hprint

Library usage

The module provides just one function, hprint or pretty_print(which is just alias of hprint), which takes a list of dict or just a single dict as the first argument, and outputs a nicely formatted plain-text table or json string as fallback.

>>> from hprint import pretty_print

>>> data = [{'name': 'John Doe', 'age': 18}, {'name': 'Jane Doe', 'age': 20}]

>>> pretty_print(data)
name        age
--------  -----
John Doe     18
Jane Doe     20

Show index

>>> pretty_print(data, numbered=True)
  No  name        age    _no
----  --------  -----  -----
   1  John Doe     18      1
   2  Jane Doe     20      2
>>> pretty_print(data, numbered=True, offset=-1)
  No  name        age    _no
----  --------  -----  -----
   0  John Doe     18      0
   1  Jane Doe     20      1

Expanded output

>>> pretty_print(data, x=True)
-[ RECORD 1 ]--+---------
name           | John Doe
age            | 18
-[ RECORD 2 ]--+---------
name           | Jane Doe
age            | 20

Customize Headers

>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'})
NAME        AGE
--------  -----
John Doe     18
Jane Doe     20
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False)
--------  --
John Doe  18
Jane Doe  20
--------  --
>>> pretty_print(data, mappings={'NAME': ('name', lambda n: n.upper()), 'AGE': 'age'})
NAME        AGE
--------  -----
JOHN DOE     18
JANE DOE     20
>>> pretty_print(data, mappings={'Aggregate': ('', lambda person: person['name'] + ": " + str(person['age']))})
Aggregate
------------
John Doe: 18
Jane Doe: 20
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False, x=True)
NAME           | John Doe
AGE            | 18
NAME           | Jane Doe
AGE            | 20

Missing value

>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'})
NAME        AGE  GENDER
--------  -----  --------
John Doe     18  n/a
Jane Doe     20  n/a
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'}, missing_value='unknown')
NAME        AGE  GENDER
--------  -----  --------
John Doe     18  unknown
Jane Doe     20  unknown

Table format

Supported format are the same with those supported by tabulate.

>>> pretty_print(data, tf='plain')
name        age
John Doe     18
Jane Doe     20
>>> pretty_print(data, tf='github')
| name     |   age |
|----------|-------|
| John Doe |    18 |
| Jane Doe |    20 |
>>> pretty_print(data, tf='pretty')
+----------+-----+
|   name   | age |
+----------+-----+
| John Doe | 18  |
| Jane Doe | 20  |
+----------+-----+
>>> pretty_print(data, tf='psql')
+----------+-------+
| name     |   age |
|----------+-------|
| John Doe |    18 |
| Jane Doe |    20 |
+----------+-------+
>>> pretty_print(data, tf='orgtbl')
| name     |   age |
|----------+-------|
| John Doe |    18 |
| Jane Doe |    20 |
>>> pretty_print(data, tf='html')
<table>
<thead>
<tr><th>name    </th><th style="text-align: right;">  age</th></tr>
</thead>
<tbody>
<tr><td>John Doe</td><td style="text-align: right;">   18</td></tr>
<tr><td>Jane Doe</td><td style="text-align: right;">   20</td></tr>
</tbody>
</table>

Print as JSON

>>> pretty_print(data, as_json=True)
[
    {
        "age": 18,
        "name": "John Doe"
    },
    {
        "age": 20,
        "name": "Jane Doe"
    }
]

Raw output

>>> s = pretty_print(data, raw=True)
>>> s
'name        age\n--------  -----\nJohn Doe     18\nJane Doe     20'

Metadata

Release files for hprint 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hprint 2.1.2
File Size Uploaded Provenance Actions
hprint-2.1.2.tar.gz 7.3 kB Without provenance Details for hprint-2.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hprint 2.1.2
File Interpreter ABI Platform Actions
hprint-2.1.2-py3-none-any.whl Python 3 none any Details for hprint-2.1.2-py3-none-any.whl

Total release size: 13.8 kB

Release files / hprint-2.1.2.tar.gz

Download URL hprint-2.1.2.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b8bb36bafbdce4f2063ca71159e39591130b5541843a6e7cb90c08d8531b5e0c
BLAKE2b-256 checksum
How to use checksums
1e26703266743f81100bc6f290d7a1c5fb19685629dd953af0014de5c4401947
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Release files / hprint-2.1.2-py3-none-any.whl

Download URL hprint-2.1.2-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e41f63b3560185926a277e2e14afdbdfcfaa66cb04333e951be24d873eae700b
BLAKE2b-256 checksum
How to use checksums
ebf7befbc44e041385a446193a908d7148199c577417ff025f15f87f414c11c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Security for hprint 2.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page