繁体   English   中英

使用Python在stdout上打印到特定行

[英]Print to a specific line on stdout using Python

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

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

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

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

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

代码看起来像这样

导入系统

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

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

或者 - 我可以写3行到stdout(使用'\\ n'),然后返回并覆盖第1行?

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

谢谢

正如@hop建议的那样, 祝福图书馆看起来很棒。

例如

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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM