I wanted to display the character string without line breaks in python, so my memo
I was displaying a message for each process for progress management, but I was annoyed that the line breaks were made once.
test.py
for i in xrange(10):
print "i = %d"%i
i=1 i=2 i=3 i=4
Like this. What I noticed here is an escape sequence called \ r. The return is represented by the reminiscent of an old typewriter. I think that this can be used
Since line breaks are not used, add a (comma) at the end of the sentence and add sleep so that you can check it.
test2.py
import time
for i in xrange(10):
print "¥r i = %d"%i,
time.sleep(0.3);
i=9
Nothing is displayed and only i = 9 is displayed. Java etc. worked well with this method, but python couldn't. Therefore, insert sys.stdout.flush () to force flush.
test3.py
import time
import sys
for i in xrange(10):
print "¥r i = %d"%i,
sys.stdout.flush()
time.sleep(0.3)
Now it works fine.
There seems to be another way to use the standard output function of python3. Also, when I looked it up, there were many articles on this issue.
Recommended Posts