Created
February 16, 2013 03:02
-
-
Save dnozay/4965322 to your computer and use it in GitHub Desktop.
prettytable and textwrap to format a dictionary (e.g. os.environ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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