I would like to make several statements that give standard output without seeing newlines in between statements.
Specifically, suppose I have:
for it
A comma at the end of the print statement omits the new line.
for i in xrange(1,100):
print i,
but this does not overwrite.
The best way to accomplish this is to use the \r
character
Just try the below code:
import time
for n in range(500):
print(n, end='\r')
time.sleep(0.01)
print() # start new line so most recently printed number stays
change
print item
to
print "\033[K", item, "\r",
sys.stdout.flush()
If you just want to print the numbers, you can avoid the loop.
# python 3
import time
startnumber = 1
endnumber = 100
# solution A without a for loop
start_time = time.clock()
m = map(str, range(startnumber, endnumber + 1))
print(' '.join(m))
end_time = time.clock()
timetaken = (end_time - start_time) * 1000
print('took {0}ms\n'.format(timetaken))
# solution B: with a for loop
start_time = time.clock()
for i in range(startnumber, endnumber + 1):
print(i, end=' ')
end_time = time.clock()
timetaken = (end_time - start_time) * 1000
print('\ntook {0}ms\n'.format(timetaken))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 took 21.1986929975ms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 took 491.466823551ms
In Python 3 you can do it this way:
for item in range(1,10):
print(item, end =" ")
Outputs:
1 2 3 4 5 6 7 8 9
Tuple: You can do the same thing with a tuple:
tup = (1,2,3,4,5)
for n in tup:
print(n, end = " - ")
Outputs:
1 - 2 - 3 - 4 - 5 -
Another example:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
for item in list_of_tuples:
print(item)
Outputs:
(1, 2)
('A', 'B')
(3, 4)
('Cat', 'Dog')
You can even unpack your tuple like this:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
# Tuple unpacking so that you can deal with elements inside of the tuple individually
for (item1, item2) in list_of_tuples:
print(item1, item2)
Outputs:
1 2
A B
3 4
Cat Dog
another variation:
list_of_tuples = [(1,2),('A','B'), (3,4), ('Cat', 'Dog')]
for (item1, item2) in list_of_tuples:
print(item1)
print(item2)
print('\n')
Outputs:
1
2
A
B
3
4
Cat
Dog
Like the other examples,
I use a similar approach but instead of spending time calculating out the last output length, etc,
I simply use ANSI code escapes to move back to the beginning of the line and then clear that entire line before printing my current status output.
import sys
class Printer():
"""Print things to stdout on one line dynamically"""
def __init__(self,data):
sys.stdout.write("\r\x1b[K"+data.__str__())
sys.stdout.flush()
To use in your iteration loop you would just call something like:
x = 1
for f in fileList:
ProcessFile(f)
output = "File number %d completed." % x
Printer(output)
x += 1
See more here