使用Python打印到标准输出上的特定行
问题描述:
我想使用Python在标准输出上打印特定行。使用Python打印到标准输出上的特定行
说..我有一个循环内循环。目前,它打印此:
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行?
或者 - 我可以写3行到标准输出(使用'\ n'),然后返回并覆盖第1行?
注意:我不只是想达到上述目的!我能做到这一点通过改变环路 - 问题是关于识别标准输出的不同行,如果可能的
感谢
答
为@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,
[传递\ n在stdout throught SYS参数(新的线)](的可能的复制http://stackoverflow.com/questions/5715414/passing-n-new -line-on-stdout-throught-sys-argument) – doctorlove
@doctorlove为什么?它看起来不是把它作为一个参数传递....或者我错过了什么?他只是使用sys的'sys.stdout.write' .... – fedepad
@doctorlove我知道关于新行感谢,它工作正常,当我使用它:)这是关于写行'X'不只是下一行( '\ n') –