简体   繁体   中英

Print the head (first few lines) of a pretty-print representation of an object

I'm running Python 2.7 in a Jupyter notebook. I am working with large nested dictionaries, and sometimes it's helpful to print out one of these.

Using pprint.pprint is a nice way of getting a readable version of the dict on screen. But for especially big dictionaries, this could mean print a million lines, which makes the notebook crash (I presume my browser is what can't handle it).

On the bash terminal I'm used to throwing things into a | head | head , but there doesn't seem to be a generic way of doing that in python.

I've written this method:

from pprint import pformat, pprint
def pprint_head(to_print,length=10)
    formatted=pformat(to_print).splitlines()
    pprint(formatted[:min(len(formatted),length)])

It works, but I wondered

  1. Is there a better/more canonical/built-in/'pythonic' way to do it?
  2. Can any of these niggles be improved? (in order of priority):
    • It's quite slow with big objects.
    • It uses a lot of memory with big objects.
    • It's pprinted as a list of strings so it has the [ at the beginning and quotes around each line.

I also wonder if there's a "Jupyter" solution (ie tell Jupyter to only accept the first x lines of any print?)

To achieve same result as a head pipe in the shell, you could easily setup an output filter in Python, because pprint only uses the write method of its stream. It could be:

class Head(object):
    def __init__(self, lines, fd=sys.stdout):
        self.lines = lines
        self.fd = fd
    def write(self, msg):
        if self.lines <= 0: return
        n = msg.count('\n')
        if n < self.lines:
            self.lines -= n
            return self.fd.write(msg)
        ix = 0
        while(self.lines > 0):
            iy = msg.find('\n', ix + 1)
            self.lines -= 1
            ix = iy
        return self.fd.write(msg[:ix])

You can then use it to pprint only the n first lines of an object that way:

def pprint_head(to_print,length=10):
    pprint(to_print, stream=Head(length))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM