Beautifying Dict Output in Python

posted

Printing dictionaries in the terminal for purposes of debugging is sometimes a last resort. Yet a last resort needs to work, that's why we do it! But printing these types of data structures is actually hard to work with because if the problem you're trying to debug is data related it may be hard for your brain to parse things that look like this:

>>> print my_dict
{u'first_name': u'Frank', u'last_name': u'Valcarcel', u'verified': True, u'name': u'Frank Valcarcel', ... }

Luckily we can improve that output tremendously with the help of a very common python library that you most likely already imported: json.

The json library has a handy printing method that uses json.dumps to indent the context when it's printed. By doing this instead you can easily parse that JSON object in a way your brain will appreciate! So start by changing your print statement to this.

>>> print(json.dumps(my_dict, indent=4))
{
    "first_name": "Frank",
    "last_name": "Valcarcel",
    "verified": true,
    "name": "Frank Valcarcel",
    "locale": "en_GB",
    "updated_time": "2014-07-15T16:26:07+0000",
    "link": "https://www.facebook.com/app_scoped_user_id/foobar/",
    "timezone": -4,
    "id": "foobar"
}


That's so much easier to read! Oh, but wait, there's more. So what about an object you might ask. We can do that by accessing the object's attribute dictionary my_obj.__dict__. An example:

class Employee:
   def __init__(self, name, salary, emp_id, section, ext, email):
      self.name = name
      self.salary = salary
      self.emp_id = emp_id
      self.section = section
      self.ext = ext
      self.email = email

# instantiate an Employee object
emp1 = Employee("Frank", 1000000000, 1, "001", "0011", "not_my@email.com")

#printing emp1 object
>>> print emp1
<__main__.Employee instance at 0x1094dcf00>

# printing dict of emp1
>>> print emp1.__dict__
{'name': 'Frank', 'salary' : 1000000000, 'emp_id' : 1, 'section' : '001', 'ext' : '0011', 'email'... }


But now we can apply our little trick to this and make it a lot easier to read!

>>> print(json.dumps(emp1.__dict__, indent=4))
{
  "name": "Frank",
  "salary" : 1000000000,
  "emp_id" : 1,
  "section" : "001",
  "ext" : "0011",
  "email" : "not_my@email.com"
}


So next time you're debugging a data related problem with your JSONs, dicts, or objects try this!

Dictionary, JSON, Python

Latest Posts

The Martian.

This book was absolutely riveting. It kept me up two nights in a row and had me imagining amber Martian landscapes around the clock. The author, Andy Weir, was previously a software ......

Pragmatic MVP, References

The Pragmatic MVP is a talk I gave at TalTech Expo 2015 on building effective early stage prototypes. Below is a list of websites, articles, and books I used in preparation...

Introducing wk

A while back I wrote about some handy aliases for virtualenv and serializing frequently and commonly chained commands that one uses to setup project environments. This is a first step at ......

Comments