我应该说我正在寻找解决查看输出不适合屏幕的问题的解决方案.例如,范围(100)将显示终端中30个高度的最后30条线.
我只是想朝着正确的方向努力,我很好奇你们是如何解决这个问题的.
当你遇到希望可以方便地滚动某些大输出的情况时,你做了什么?
使用终端上的回滚缓冲区.
如果你使用的是GNU Screen,可以在其中设置defscrollback 1000或使用任何其他数字HOME/.screenrc.
使用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
Run Code Online (Sandbox Code Playgroud)
最好的部分是?反向搜索,/在复制模式下进行前向搜索.
超级方便.
谢谢!
什么是python相当于bash less命令?
LongString | less
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
一个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
Run Code Online (Sandbox Code Playgroud)
如果您想在交互式 Python 会话中执行此操作,则应使用允许向上滚动的终端模拟。我相信他们中的大多数人都是如此。
如果您使用的是实际终端,或者您没有终端模拟器的选择,也许您可以使用GNU screen。
(如果您使用的是 Windows,则可以更改屏幕缓冲区大小以允许向后滚动最多 9999 行)。
如果你的程序输出需要这个,你可以尝试使用curses模块来自己实现滚动。