Code Monkey home page Code Monkey logo

pycolumnize's Introduction

Linux Build Status Pypi Installs Windows Build status Supported Python Versions

packagestatus

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.

pycdio for enterprise

Available as part of the Tidelift Subscription.

The maintainers of pycdio 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 [email protected]

License: MIT

pycolumnize's People

Contributors

martica avatar msabramo avatar rocky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.