English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The main problem with the progress bar is that all characters are on the same line and can be modified.
However, when executing the print statement, Python will add a '\n' at the end of the statement, which is a newline, causing the output in the console to be uneditable after being printed. Therefore, we can no longer use print to complete the output.
We will use the sys.stdout.write() function from the sys library, which will output the string to the console without adding any ending, which means that the output has not been completely finished. By using the sys.stdout.flush() function, the output can be temporarily printed on the console (creating the illusion of print, let's call it pseudo-print for the time being). So if we use the escape character 'r' (go back to the beginning), doesn't everything look more reasonable?
That is to say: when printing a string, no '\n' is added, and the cursor is returned to the beginning of the line, and then the current buffer is displayed, which is just like a print, but at this time the cursor is still in the original position.
An example
[Tested to be effective only in Linux terminals, the debug output in pycharm does not work]
import sys, time for i in range(5) sys.stdout.write('HELLO: %05d' % i) sys.stdout.flush() time.sleep(1)
Running this code in the terminal will produce a simple progress bar effect.
Next, we still need to solve two problems:
First: Clear the Buffer
Some clever readers may have noticed that there will be problems when the new string is shorter than the previous one, such as the following code:
import sys, time for i in range(5) sys.stdout.write(str(i) * (5 - i) + '\r') sys.stdout.flush() time.sleep(1)
After running, the result is not quite what we expected.
Actually, it is because the characters that have been flushed out will not be cleared automatically, so only the newly written ones are modified. In response to this, my current solution is to output a series of spaces to push off the previous string and then rewrite it:
import sys, time for i in range(5) sys.stdout.write(' ') * 10 + '\r') sys.stdout.flush() sys.stdout.write(str(i) * (5 - i) + '\r') sys.stdout.flush() time.sleep(1)
Second: Fixed Bottom Output
Sometimes we may want to have some other outputs while the progress bar is loading.
Let's output the required string after refreshing the previous output and then output the pseudo progress bar.
Using the following code:
import sys, time for i in range(5) sys.stdout.write(' ') * 10 + '\r') sys.stdout.flush() print i sys.stdout.write(str(i) * (5 - i) + '\r') sys.stdout.flush() time.sleep(1)
and you can complete the required task.
How about it? The principle is actually quite simple, isn't it?
Here is a class implemented by myself to print the progress bar:
import sys, time class ProgressBar: def __init__(self, count = 0, total = 0, width = 50): self.count = count self.total = total self.width = width def move(self): self.count += 1 def log(self, s): sys.stdout.write(' ') * (self.width + 9) + '\r') sys.stdout.flush() print s progress = self.width * self.count / self.total sys.stdout.write('{0:3}/{1:3}: '.format(self.count, self.total)) sys.stdout.write('#' * progress + '-' * (self.width - progress) + '\r') if progress == self.width: sys.stdout.write('\n') sys.stdout.flush() bar = ProgressBar(total = 10) for i in range(10) bar.move() bar.log('We have arrived at: ') + str(i + 1)) time.sleep(1)
The effect is as follows:
This can make it convenient to view the progress of program execution in some tasks, such as crawlers, machine learning, and other work that does not know how much time it will take, and can also have a tangible time grasp.
The above code for implementing a progress bar function in the console in Python is all the content the editor shares with everyone. I hope it can be a reference for everyone, and I also hope everyone will support the Yelling Tutorial.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)