Python——如何查看不适合屏幕的输出?

2024-01-08

我应该说我正在寻找问题的解决方案查看不适合您屏幕的输出。例如,range(100) 将显示高度为 30 的终端中的最后 30 行。

我只是希望被推向正确的方向,并且很好奇你们是如何解决这个问题的。

当您遇到希望可以方便地滚动浏览一些大输出的情况时,您会做什么?

最佳答案

使用终端上的回滚缓冲区。

如果您使用的是 GNU Screen,则可以使用以下命令进行设置defscrollback 1000或任何其他数字HOME/.screenrc.

Use Ctrl-a, [进入复印模式

j -    Move the cursor down by one line
k -    Move the cursor up by one line
C-u -  Scrolls a half page up.
C-b -  Scrolls a full page up.
C-d -  Scrolls a half page down.
C-f -  Scrolls the full page down.
G -    Moves to the specified line 

最好的部分是?对于反向搜索,/用于在复制模式下向前搜索。

超级方便。

谢谢你!


原问题:

bash less 命令在 python 中的等价物是什么?

LongString | less 

python 有类似的东西吗?我发现自己认为我可以经常使用它,但继续寻找其他解决方案。

我所说的“长东西”是指生成比屏幕更多的输出行的任何东西。 1000 条打印消息、字典、大字符串、范围(1000)等。

我的 googlefu 失败了。


只是为了好玩:o)

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 <Enter> for more")

less = Less(num_lines=30)  
"\n".join(map(str, range(100))) | less

Python3版本:

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 <Enter> for more")

less = Less(num_lines=30)  
"\n".join(map(str, range(100))) | less
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python——如何查看不适合屏幕的输出? 的相关文章

随机推荐