<p>如果我正确地理解了您的问题,您想知道如何在终端中移动光标。不幸的是,没有一种简单、便携的方法可以做到这一点。{/a1}就是这样做的。IIRC,Windows也可以使用这样的序列,但默认情况下是禁用的。在</p>
<p>总之,这里有一个小的演示脚本。注意,这个脚本可能不完全适用于所有*nix系统,这取决于它们使用的终端的确切细节;但是我将让Unix终端专家提供更正。:)</p>
<pre class="lang-python prettyprint-override"><code>#! /usr/bin/env python
''' Simple demo of using ANSI escape codes to move the cursor '''
import sys
from time import sleep
from string import ascii_letters
#ANSI Control Sequence Introducer
csi = '\x1b['
def put(s): sys.stdout.write(s)
#Build a simple table row
def row(c, m, n):
return '| ' + ' | '.join(n * [m*c]) + ' |'
def main():
#Some data to make a table with
data = ascii_letters
#The number of rows per table section
numrows = 6
#Adjust data length to a multiple of numrows
newlen = (len(data) // numrows) * numrows
data = data[:newlen]
m, n = 5, 7
width = (m + 3) * n + 4
print 'Table'.center(width, '-')
for i, c in enumerate(data):
if i and not i % numrows:
sleep(2)
#Move cursor up by numrows
put('%s%dA' % (csi, numrows))
print "%2d %s" % (i, row(c, m, n))
if __name__ == '__main__':
main()
</code></pre>