Format a simple (i.e. not nested) list into aligned columns.
Project description
In showing a long lists, sometimes one would prefer to see the value arranged aligned in columns. Some examples include listing methods of an object, listing debugger commands, or showing a numeric array with data aligned.
This is a Python module to format a simple (i.e. not nested) list into aligned columns. A string with embedded newline characters is returned.
Setup
$ python >>> import columnize
With String data
Each column is only as wide as necessary. By default, columns are separated by two spaces; one was not legible enough. Set colsep to adjust the string separate columns. Set displaywidth to set the line width.
>>> g = ('bibrons', 'golden', 'madascar', 'leopard', 'mourning', 'suras', 'tokay') >>> print(columnize.columnize(g, displaywidth=15)) bibrons suras golden tokay madascar leopard mourning>>> print(columnize.columnize(g, displaywidth=19, colsep=' | ')) bibrons | mourning golden | suras madascar | tokay leopard>>> print(columnize.columnize(g, displaywidth=18, colsep=' | ', ljust=False)) bibrons | suras golden | tokay madascar leopard
Normally, consecutive items go down from the top to bottom from the left-most column to the right-most. If arrange_vertical is set false, consecutive items will go across, left to right, top to bottom.
With numeric data
>>> print(columnize.columnize(['1', '2', '3', '4'], displaywidth=6)) # => '1 3\n2 4\n' 1 3 2 4>>> print(columnize.columnize(list(range(1,6)), displaywidth=8)) 1 3 5 2 4
By default entries are left justified:
>>> print(columnize.columnize(list(range(1,16)), displaywidth=10))1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
but you can change that with ljust or if arrange_array is set to True:
>>> print(columnize.columnize(list(range(1,16)), displaywidth=10, ljust=False)) 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15>>> print(columnize.columnize(list(range(1,5)), opts={'arrange_array':True, 'displaywidth':6})) [1, 2 3, 4]
Credits
This module (essentially one function) was adapted from a private method of the same name from Python’s cmd module. Some adjustments and generalizations have been made.
pycolumnize for enterprise
Available as part of the Tidelift Subscription.
The maintainers of pycolumnize and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
Security contact information
To report a security vulnerability, please use the Tidelift security contact and Tidelift will coordinate the fix and disclosure.
Other stuff
Authors: Rocky Bernstein rb@dustyfeet.com
License: MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for columnize-0.3.11-py36-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10dc33cbe4ef96b777b407a96078cea2b195acfaf86592357e318180c81a83d9 |
|
MD5 | 65a5929812dcb44da13bf8a5e636ed79 |
|
BLAKE2b-256 | ddc39902c94a4835f64d7151c6c5c40687de1438f37118d9270d15b5a2212ab1 |
Hashes for columnize-0.3.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c474ca632e474df8f012843fb74a4c0bea4e8876d49a17f952f8e82f14cbc3d |
|
MD5 | c0b061531533a05a2b5ca83c6ff5f278 |
|
BLAKE2b-256 | dedac1f84838ecb3e84c3c85c691f51f1e2dfcf7be745761d9f6462c909175ad |
Hashes for columnize-0.3.11-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4f5f53eadaddfa7bd57150d6f0ed8c7e704b82b196b17290f97fc1f5422bb54 |
|
MD5 | e97b7972b01f7e05eeacaa8540077359 |
|
BLAKE2b-256 | 8a3fd47e953bcc8ff5b8b5f6ddcf71b2fc5d9083c54fa8ccbe76883cf8a2bf4c |