使用Python

时间:2017-02-10 11:39:52

标签: python stdout

我想使用Python打印到stdout上的特定行。

说..我在循环中有一个循环。目前打印出来:

a0,b0,c0,a1,b1,c1,a2,b2,c2,

但实际上我希望它打印出来:

a0,a1,a2,
b0,b1,b2,
c0,c1,c2,

代码看起来像这样

导入sys

ar = ['a', 'b', 'c']
for i in ar:
    c = 1
    while c < 4:
        sys.stdout.write('%s%s,' % (i, c))
        c += 1

有没有办法识别这条线?例如,打印到X行?

或者 - 我可以向stdout写3行(使用&#39; \ n&#39;),然后返回并覆盖第1行?

注意:我不想实现上述目标!我可以通过改变循环来做到这一点 - 问题是如何识别stdout的不同行并写入它们

由于

1 个答案:

答案 0 :(得分:0)

正如@hop建议的那样,blessings库看起来很棒。

例如

from blessings import Terminal

term = Terminal()
with term.location(0, term.height - 1):
    print 'Here is the bottom.'

在我的例子中,下面的内容有效。它确实提供了我正在寻找的输出。

from blessings import Terminal

term = Terminal()

print '.'
print '.'
print '.'


ar = ['a', 'b', 'c']
x = 1
for i in ar:
    c = 1
    while c < 4:
        with term.location(c*3-3, term.height - (5-x)):
            print str(i)+str(c-1)+','
        c += 1   
    x += 1

给出:

a0,a1,a2,
b0,b1,b2,
c0,c1,c2,