打印语句的分页输出 [英] Paging output from print statement

查看:73
本文介绍了打印语句的分页输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是在努力实现这一目标:

I'm essentially trying to achieve this:

>>>print "SOME_VERY_LONG_TEXT" | more

当然,它不适用于 Python 2.7(IDLE).

Of course, it doesn't work in Python 2.7(IDLE).

另外,我尝试了 pager 1.2page() 函数,但我不知道如何让它正常工作.

Also, I tried pager 1.2's page() function, but I don't know how to get it to work correctly.

有什么想法吗?

[更新]

我找到了一个懒人的方法,如下:

I found a lazy way, as follows:

import pydoc
pydoc.pager("SOME_VERY_LONG_TEXT") 

推荐答案

编写一些与终端和操作系统无关的内容可能是一项更大的任务.

Writing something terminal and os independent might be a bigger task.

但是如果你能得到终端的高度,那么你可以使用这样的东西假设您的输入是一个生成器/行分隔文本列表,否则您可以在调用此函数之前调用 text.split('\n')

But if you can get the height of the terminal then you can use something like this this Assuming your input is a generator/list of line seperated text, or else you can call text.split('\n') before calling this function

def pagetext(text_lined, num_lines=25):
   for index,line in enumerate(text_lined):
       if index % num_lines == 0 and index:
           input=raw_input("Hit any key to continue press q to quit")
           if input.lower() == 'q':
               break
       else:
           print line

还有一个pager pypy 模块没用过但是作者说应该包含在标准库中.

Also there is a pager module on pypy haven't used it but the author says it was supposed to be included in the standard library.

这篇关于打印语句的分页输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆