AverageSecurityGuy

Security, Programming, Pentesting

About

Mastodon

Linked In

Projects

Cheat Sheets

Book

28 November 2012

Text Tables in Python

by {"login"=>"averagesecurityguy", "email"=>"stephen@averagesecurityguy.info", "display_name"=>"averagesecurityguy", "first_name"=>"", "last_name"=>""}

At Tenable, I write text-based, single-purpose API tools in Python. I often need to display data in table format and wanted an easy way to do this. So, I wrote a simple Python module to draw text tables. Texttable allows you to add a header, column names, and specify column alignments. You can get the code here.

Usage

import texttable

t1 = texttable.TextTable()
t1.header = 'A Table of Numbers'
t1.add_col_names(['Col1', 'Col2', 'Col3', 'Col4'])
t1.add_col_align(['<', '<', '^', '>'])
rows = [[1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [111111, 22222222, 3333333333, 444444444444]]
t1.add_rows(rows)
print t1
    
t2 = texttable.TextTable()
t2.header = 'Another Table of Numbers'
t2.add_col_names(['Col1', 'Col2', 'Col3', 'Col4'])
t2.add_row([1, 2, 3, 4])
t2.add_row([5, 6, 7, 8])
t2.add_row([9, 10, 11, 12])
print t2
tags: python