Python Tricks - Dictionary Tricks(5)

Dictionary Pretty-Printing

Have you ever tried hunting down a bug in one of your programs by sprinkling a bunch of debug “print” statements to trace the execution flow? Or maybe you needed to generate a log message to print some configuration settings…

I have—and I’ve often been frustrated with how difficult some data structures are to read in Python when they’re printed as text strings. For example, here’s a simple dictionary. Printed in an interpreter session, the key order is arbitrary, and there’s no indentation to the resulting string:

>>> mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
>>> str(mapping)
{'b': 42, 'c': 12648430, 'a': 23}

Luckily there are some easy-to-use alternatives to a straight up tostr conversion that give a more readable result. One option is using Python’s built-in json module. You can use json.dumps() to prettyprint Python dicts with nicer formatting:

>>> import json
>>> json.dumps(mapping, indent=4, sort_keys=True)
{
  "a": 23,
  "b": 42,
  "c": 12648430
}

These settings result in a nicely indented string representation that also normalizes the order of the dictionary keys for better legibility.

While this looks nice and readable, it isn’t a perfect solution. Printing dictionaries with the json module only works with dicts that contain primitive types—you’ll run into trouble trying to print a dictionary that contains a non-primitive data type, like a function:

>>> json.dumps({all: 'yup'})
TypeError: "keys must be a string"

键一定是字符串 这是json对于格式的要求

Another downside of using json.dumps() is that it can’t stringify complex data types, like sets:

>>> mapping['d'] = {1, 2, 3}
>>> json.dumps(mapping)
TypeError: "set([1, 2, 3]) is not JSON serializable"

json不能字符化复杂的数据类型,比如集合。

Also, you might run into trouble with how Unicode text is represented—in some cases you won’t be able to take the output from json.dumps and copy and paste it into a Python interpreter session to reconstruct the original dictionary object.

The classical solution to pretty-printing objects in Python is the builtin pprint module. Here’s an example:

>>> import pprint
>>> pprint.pprint(mapping)
{'a': 23, 'b': 42, 'c': 12648430, 'd': set([1, 2, 3])}

You can see that pprint is able to print data types like sets, and it also prints the dictionary keys in a reproducible order. Compared to the standard string representation for dictionaries, what we get here is much easier on the eyes.

However, compared to json.dumps(), it doesn’t represent nested structures as well visually. Depending on the circumstances, this can be an advantage or a disadvantage. I occasionally use json.dumps() to print dictionaries because of the improved readability and formatting, but only if I’m sure they’re free of non-primitive data types.

Key Takeaways
  • The default to-string conversion for dictionary objects in Python can be difficult to read.
  • The pprint and json module are “higher-fidelity” options built into the Python standard library.
  • Be careful with using json.dumps() and non-primitive keys and values as this will trigger a TypeError.

转载于:https://www.jianshu.com/p/b6748e9e1dce

猜你喜欢

转载自blog.csdn.net/weixin_33834075/article/details/91186831