Skip to main content
Help us improve Python packaging – donate today!

Format a simple (i.e. not nested) list into aligned columns.

Project Description

Downloads Build Status Latest Version Supported Python versions

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.

Other stuff

Authors: Rocky Bernstein rockyb@rubyforge.org

License: MIT

Release history Release notifications

This version
History Node

0.3.9

History Node

0.3.8

History Node

0.3.7

History Node

0.3.6

History Node

0.3.5-01

History Node

0.3.5

History Node

0.3.4

History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3.0

History Node

0.2.0

History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
columnize-0.3.9-py2.4.egg (8.9 kB) Copy SHA256 hash SHA256 Egg 2.4 Aug 2, 2017
columnize-0.3.9-py2.5.egg (8.8 kB) Copy SHA256 hash SHA256 Egg 2.5 Aug 2, 2017
columnize-0.3.9-py2.6.egg (8.9 kB) Copy SHA256 hash SHA256 Egg 2.6 Mar 19, 2016
columnize-0.3.9-py2.7.egg (8.8 kB) Copy SHA256 hash SHA256 Egg 2.7 Mar 19, 2016
columnize-0.3.9-py2-none-any.whl (7.6 kB) Copy SHA256 hash SHA256 Wheel 2.6 Mar 19, 2016
columnize-0.3.9-py3.2.egg (8.9 kB) Copy SHA256 hash SHA256 Egg 3.2 Mar 19, 2016
columnize-0.3.9-py3.3.egg (8.8 kB) Copy SHA256 hash SHA256 Egg 3.3 Mar 19, 2016
columnize-0.3.9-py3-none-any.whl (7.6 kB) Copy SHA256 hash SHA256 Wheel 3.2 Mar 19, 2016
columnize-0.3.9.tar.gz (8.6 kB) Copy SHA256 hash SHA256 Source None Mar 19, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page