Python — How do you view output that doesn't fit the screen?

前端 未结 9 1894
挽巷
挽巷 2021-02-05 10:27

I should say I\'m looking for a solution to the problem of viewing output that does not fit on your screen. For example, range(100) will show the last 30ish lin

9条回答
  •  天命终不由人
    2021-02-05 11:06

    just for fun :o)

    Original version for Python2:

    class Less(object):
        def __init__(self, num_lines):
            self.num_lines = num_lines
        def __ror__(self, other):
            s = str(other).split("\n")
            for i in range(0, len(s), self.num_lines):
                print "\n".join(s[i: i + self.num_lines])
                raw_input("Press  for more")
    
    less = Less(num_lines=30)  
    "\n".join(map(str, range(100))) | less
    

    a Python3 version:

    class Less(object):
        def __init__(self, num_lines):
            self.num_lines = num_lines
        def __ror__(self, other):
            s = str(other).split("\n")
            for i in range(0, len(s), self.num_lines):
                print(*s[i: i + self.num_lines], sep="\n")
                input("Press  for more")
    
    less = Less(num_lines=30)  
    "\n".join(map(str, range(100))) | less
    

提交回复
热议问题