# Pretty printing (using the pprint module) transforms # python objects into human readable output. # # Use pprint when you need to display a complex # data structure to users. import string import pprint # pprint python doc d = {} for i in string.ascii_lowercase: d[i] = string.ascii_lowercase.replace(i, ' ') print "not useful output:" print d # output: # not useful output: # {'a': ' bcdefghijklmnopqrstuvwxyz', 'c': 'ab defghijklmnopqrstuvwxyz', 'b': # 'a cdefghijklmnopqrstuvwxyz', 'e': 'abcd fghijklmnopqrstuvwxyz', 'd': 'abc # efghijklmnopqrstuvwxyz', 'g': 'abcdef hijklmnopqrstuvwxyz', 'f': 'abcde ghij # klmnopqrstuvwxyz', 'i': 'abcdefgh jklmnopqrstuvwxyz', 'h': 'abcdefg ijklmnop # qrstuvwxyz', 'k': 'abcdefghij lmnopqrstuvwxyz', 'j': 'abcdefghi klmnopqrstuv # wxyz', 'm': 'abcdefghijkl nopqrstuvwxyz', 'l': 'abcdefghijk mnopqrstuvwxyz', # 'o': 'abcdefghijklmn pqrstuvwxyz', 'n': 'abcdefghijklm opqrstuvwxyz', 'q': ' # abcdefghijklmnop rstuvwxyz', 'p': 'abcdefghijklmno qrstuvwxyz', 's': 'abcdef # ghijklmnopqr tuvwxyz', 'r': 'abcdefghijklmnopq stuvwxyz', 'u': 'abcdefghijkl # mnopqrst vwxyz', 't': 'abcdefghijklmnopqrs uvwxyz', 'w': 'abcdefghijklmnopqr # stuv xyz', 'v': 'abcdefghijklmnopqrstu wxyz', 'y': 'abcdefghijklmnopqrstuvwx # z', 'x': 'abcdefghijklmnopqrstuvw yz', 'z': 'abcdefghijklmnopqrstuvwxy '} # # All the data is there but it is difficult to read. # You can use pprint (pretty print) to make things easy to read. pprint # formats python datastructures to be human readable. print "human readable output:" pprint.pprint(d, indent=4) # output: #human readable output: #{ 'a': ' bcdefghijklmnopqrstuvwxyz', # 'b': 'a cdefghijklmnopqrstuvwxyz', # 'c': 'ab defghijklmnopqrstuvwxyz', # 'd': 'abc efghijklmnopqrstuvwxyz', # 'e': 'abcd fghijklmnopqrstuvwxyz', # 'f': 'abcde ghijklmnopqrstuvwxyz', # 'g': 'abcdef hijklmnopqrstuvwxyz', # 'h': 'abcdefg ijklmnopqrstuvwxyz', # 'i': 'abcdefgh jklmnopqrstuvwxyz', # 'j': 'abcdefghi klmnopqrstuvwxyz', # 'k': 'abcdefghij lmnopqrstuvwxyz', # 'l': 'abcdefghijk mnopqrstuvwxyz', # 'm': 'abcdefghijkl nopqrstuvwxyz', # 'n': 'abcdefghijklm opqrstuvwxyz', # 'o': 'abcdefghijklmn pqrstuvwxyz', # 'p': 'abcdefghijklmno qrstuvwxyz', # 'q': 'abcdefghijklmnop rstuvwxyz', # 'r': 'abcdefghijklmnopq stuvwxyz', # 's': 'abcdefghijklmnopqr tuvwxyz', # 't': 'abcdefghijklmnopqrs uvwxyz', # 'u': 'abcdefghijklmnopqrst vwxyz', # 'v': 'abcdefghijklmnopqrstu wxyz', # 'w': 'abcdefghijklmnopqrstuv xyz', # 'x': 'abcdefghijklmnopqrstuvw yz', # 'y': 'abcdefghijklmnopqrstuvwx z', # 'z': 'abcdefghijklmnopqrstuvwxy '} # # Formatted in this fashion its easy to see what # data is being stored in the dict.
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label PrettyPrinter. Show all posts
Showing posts with label PrettyPrinter. Show all posts
Thursday, October 1, 2009
Python - printing complex objects with pretty printing
Labels:
dict,
pprint,
PrettyPrinter,
python,
string
Subscribe to:
Posts (Atom)