Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created February 16, 2013 03:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnozay/4965322 to your computer and use it in GitHub Desktop.
Save dnozay/4965322 to your computer and use it in GitHub Desktop.
prettytable and textwrap to format a dictionary (e.g. os.environ)
# some simple function that uses prettytable and textwrap to format a dictionary
# http://code.google.com/p/prettytable/wiki/Tutorial
from prettytable import PrettyTable
from textwrap import wrap
VAL_WRAP_WIDTH = 60
def pretty_dictionary(dic):
tab = PrettyTable(['key', 'value'])
for key, val in dic.items():
wrapped_value_lines = wrap(str(val) or '', VAL_WRAP_WIDTH) or ['']
tab.add_row([key, wrapped_value_lines[0]])
for subseq in wrapped_value_lines[1:]:
tab.add_row(['', subseq])
return tab
# e.g print pretty_dictionary(os.environ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment