Python -- 如何查看屏幕外的输出?
我想说我在寻找一个解决方案,来处理输出内容超出屏幕显示的问题。 比如,使用range(100)时,终端只会显示最后大约30行,而我的终端高度只有30行。
我只是想得到一些指引,想知道你们是怎么解决这个问题的。
当你遇到需要方便地滚动查看大量输出的情况时,你都做了些什么呢?
最佳答案
可以使用终端的滚动缓冲区。
如果你在用GNU Screen,可以在HOME/.screenrc
中设置defscrollback 1000
,或者其他数字。
使用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
最棒的是,在复制模式下可以用?
进行反向搜索,用/
进行正向搜索。
超级方便。
谢谢!
原问题:
Python中有没有类似bash的less命令?
LongString | less
Python有没有这样的东西?我发现自己经常想用它,但最后还是找了其他解决方案。
我所说的“长内容”是指任何输出行数超过我屏幕的东西,比如1000条打印信息、一个字典、一段大字符串、range(1000)等等。
我在谷歌上找不到答案。
9 个回答
是的,有一种方法,而且非常简单,所以没有特别详细说明。假设你有一个很长的列表,想要只查看开头的部分:
>>> lst = range(1000) # let's make list of thousand elements
>>> lst[:100] # i want to see just the first 100
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> lst[100:201] # ok, now the 2nd hundred
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200]
>>> lst[-100:] # and now just the last 100, pretty please?
[900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999]
同样的 []
切片方法在字符串上也可以使用。对于字典,你可以用一种变通的方法,比如使用 str()
把它转换成字符串,然后用同样的切片方式来显示 :-)
纯粹为了好玩 :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
如果你想在交互式的Python环境中实现这个功能,你需要使用一个可以向上滚动的终端模拟器。我想大部分终端模拟器都支持这个功能。
如果你在使用真正的终端,或者没有其他终端模拟器可以选择,你可以试试GNU screen这个工具。
如果你是在Windows系统上,可以调整屏幕缓冲区的大小,这样就可以向上滚动最多9999行。
如果你需要在程序的输出中实现这个功能,可以尝试使用curses模块来自己实现滚动效果。