I donโt know a good solution for arbitrary Unicode, as some Unicode can be too narrow or wide, and even monospaced fonts do not seem to represent all Unicode as having the same width (see example below).
However, this variation on the George Sakkis table descriptor recipe may suffice:
Save this in table.py file:
import operator import itertools import re import math import functools import logging logger = logging.getLogger(__name__) zip_longest = itertools.izip_longest def tableinfo(rows, sep = u'โ', corner = u'ยท', delim = None, corner_delim = None, prefix = u'โ ', postfix = u' โ', colsep = u' โ ', has_header = False, header = None, separate_rows = False, framed = (True, True), separate_empty_lines = True, justify = 'right', wrapfunc = lambda x:x, width = None, phantom = None, **kw):
Then you can use it as follows:
import table data = [ ('id', 'now()', 'aaa'), ('28', '2012-03-01 14:24:02', u'To \N{INFINITY} and beyond'), ('77', '2012-03-01 14:24:02', u"All with me meet"), ('89', '2012-03-01 14:24:02', u' that I can fashion fit \N{BLACK SMILING FACE}'), ('114', '2012-03-01 14:24:02', u'\N{LATIN CAPITAL LETTER OU}'), ('116', '2012-03-01 14:24:02', u'An extra wide unicode: \N{COMBINING CYRILLIC HUNDRED THOUSANDS SIGN}'), ('252', '2012-03-01 14:24:02', u'\N{THEREFORE}'), ] print(table.ascii_table(data, has_header = True))
what gives
+-----+---------------------+---------------------------+ | id | now() | aaa | +-----+---------------------+---------------------------+ | 28 | 2012-03-01 14:24:02 | To โ and beyond | | 77 | 2012-03-01 14:24:02 | All with me meet | | 89 | 2012-03-01 14:24:02 | that I can fashion fit โป | | 114 | 2012-03-01 14:24:02 | ศข | | 116 | 2012-03-01 14:24:02 | An extra wide unicode: า | | 252 | 2012-03-01 14:24:02 | โด | +-----+---------------------+---------------------------+
Note that the lines corresponding to id = 114 and 116 do not quite match the others.
(Read the docstring for table.table for more information on the options available.)