Initially I was using the following line in my script (python 3):
print ('{0}\t{1:.2f}\t{2:.8f}\t{3}'.format(var1, var2, var3, var4), end='\r')
The output gets updated in the same line. This is exactly the behavior that I want.
However due to some issues in a library that I'm using, I've changed my code to python 2.
I used the following import statement:
from __future__ import print_function
at the top of the file. But it is not printing anything in my terminal. I have tried tinkering with the print statement but it always ends up printing in a new line.
Can someone please tell me the exact equivalent for the above statement in python 2? (Using from __future__ import print_function
is allowed).
Update: The first two lines of my code are:
# -*- coding: utf-8 -*-
from __future__ import print_function
There is no line before these two.
One of the variables in the print statement contains the ↑
character. I do not know if that could be the cause of the problem.
Update 2: As pointed out by https://stackoverflow.com/users/1639625/tobias-k in the comments, the behaviour of the print
function changed with Python 3.3 and that change was not backported. Hence, we can't get it to work in python 2. An alternate way to do the same is given in the linked question using stdout.flush
and stdout.write
.