2

I would like to print to a specific line on stdout using Python.

Say.. I have a loop within a loop. Currently it prints this :

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

But actually I want it to print this :

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

the code looks something like this

import sys

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

Is there a way of identify the line ? eg print to line X ?

Or - can I write 3 lines to stdout (using '\n'), then go back and overwrite line 1 ?

note: I don't just want to achieve the above ! I can do that by altering the loop - the question is about identifying different lines of stdout and writing to them, if possible

Thanks

jowan sebastian
  • 253
  • 2
  • 11
  • 1
    Possible duplicate of [passing \n (new line) on stdout throught sys argument](http://stackoverflow.com/questions/5715414/passing-n-new-line-on-stdout-throught-sys-argument) – doctorlove Feb 10 '17 at 11:44
  • @doctorlove Why? It looks like is not passing it as an argument....or am I missing something? He just uses sys for `sys.stdout.write`.... – fedepad Feb 10 '17 at 11:48
  • @doctorlove I know about new line thanks, it works fine when I use it :) this is about writing to line 'X' not just the next line ('\n') – jowan sebastian Feb 10 '17 at 11:49
  • I was just checking if @jowansebastian was after `\n` or correct encoding or something else. – doctorlove Feb 10 '17 at 11:54
  • Ah! `\r` will let you overwrite the current line, e.g. http://stackoverflow.com/questions/517127/how-do-i-write-output-in-same-place-on-the-console - let's see what anyone else comes up with – doctorlove Feb 10 '17 at 11:59
  • Yes, but this only does the last line. In the full example I am using this, I'm doing multi threading progress bars. But I want to overwrite a specific line, not just the last one. – jowan sebastian Feb 10 '17 at 12:18
  • which python? also you probably mean terminal not stdout right? take a look at curses – user3012759 Feb 10 '17 at 12:19
  • @user3012759 yes I'm printing to Terminal, but I will also want to pick it up if executed via ssh etc. I'll look at curses now – jowan sebastian Feb 10 '17 at 12:21
  • use blessings [http://pythonhosted.org/blessings/], not curses –  Feb 10 '17 at 13:48
  • downvote, because the whole bit about loops is an unnecessary distraction. –  Feb 10 '17 at 13:48
  • 1
    @hop Understood, but it's quite a good analogy to what I am actually doing with multi-threading. You can consider the outer loops like the separate threads. – jowan sebastian Feb 10 '17 at 15:46
  • @hop will check out Blessings. You're link it badly formatted though. – jowan sebastian Feb 10 '17 at 15:47

1 Answers1

1

as @hop suggested, the blessings library looks great for this.

For example

from blessings import Terminal

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

and in my example, something along the lines of the below works. It does give the output I was looking for.

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

gives:

a0,a1,a2,
b0,b1,b2,
c0,c1,c2,
jowan sebastian
  • 253
  • 2
  • 11